解析嵌套的 json 和 javascript
Parsing nested json with javascript
我有一些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;
});
我有一些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;
});