我有一些 JSON。
{
ZVH2: {
username: 'ZVH2',
ping: 0,
uuid: '3a4423c3-dce1-40c1-8333-ab2ffdfcd005',
displayName: ChatMessage {
json: [Object],
text: '',
extra: [Array],
bold: undefined,
italic: undefined,
underlined: undefined,
strikethrough: undefined,
obfuscated: undefined,
color: undefined
},
entity: Entity {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
id: 367,
type: 'player',
position: [Vec3],
velocity: [Vec3],
yaw: 3.141592653589793,
pitch: 0,
onGround: false,
height: 1.62,
width: 0,
effects: [Object],
equipment: [Array],
heldItem: [Item],
isValid: true,
metadata: [Array],
username: 'ZVH2',
name: 'player',
timeSinceOnGround: 0,
attributes: [Object],
isInWater: false,
isInLava: false,
isInWeb: undefined,
isCollidedHorizontally: false,
isCollidedVertically: false,
[Symbol(kCapture)]: false
},
gamemode: 2
},
Maximo237354: {
username: 'Maximo237354',
ping: 161,
uuid: 'd6d1bcda-d3c0-406b-b91e-beb3f7be9f5f',
displayName: ChatMessage {
json: [Object],
text: '',
extra: [Array],
bold: undefined,
italic: undefined,
underlined: undefined,
strikethrough: undefined,
obfuscated: undefined,
color: undefined
},
entity: Entity {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
id: 72,
type: 'player',
position: [Vec3],
velocity: [Vec3],
yaw: 2.773437264497239,
pitch: -0.04908738521234035,
onGround: true,
height: 1.62,
width: 0.6,
effects: [Object],
equipment: [Array],
heldItem: [Item],
isValid: true,
metadata: [Array],
name: 'player',
username: 'Maximo237354',
uuid: 'd6d1bcda-d3c0-406b-b91e-beb3f7be9f5f',
dataBlobs: undefined,
attributes: [Object],
headYaw: 2.773437264497239,
[Symbol(kCapture)]: false
}
}
}
我想在這裡使用 javascript 打印 username 的用戶名。它可以遍歷每件事並打印用戶名值,或者它可以只取第一個值,因為那也是用戶名。我在網上找不到任何好的教程。我想確保我打印每個用戶名而不是實體。有人可以幫忙嗎?
假設您的 JSON 變量被調用data
,您可以使用Object.keys(data)
迭代並獲取您可以執行的用戶名
Object.keys(data).forEach(key => {
let username = data[key].username;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句