简体   繁体   English

用javascript解析嵌套的json

[英]Parsing nested json with javascript

I have some JSON.我有一些 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
    }
  }
}

I want to use javascript to print the username of username in here.我想在这里使用 javascript 打印 username 的用户名。 It can either iterate through each thing and print the username value, or it can just take the first value because that is the username too.它可以遍历每件事并打印用户名值,或者它可以只取第一个值,因为那也是用户名。 I can't find a good tutorial online anywhere for this.我在网上找不到任何好的教程。 I want to make sure that I print every username and not entity.我想确保我打印每个用户名而不是实体。 Can someone please help with this?有人可以帮忙吗?

Assuming your JSON variable is called data , you can extract the keys with Object.keys(data)假设您的 JSON 变量名为data ,您可以使用Object.keys(data)提取键

To iterate and get the usernames you can do迭代并获取您可以执行的用户名

Object.keys(data).forEach(key => {
   let username = data[key].username;
});

Please understand that JSON stands for JavaScript Object Notation.请理解 JSON 代表 JavaScript Object Notation。 In other words, JSON represents a Javascript object.换句话说,JSON 代表一个 Javascript 对象。 Convert the JSON to a Javascript object.将 JSON 转换为 Javascript 对象。

Eg例如

var object1 = JSON.parse('{"rollno":101, "name":"Mayank", "age":20}'); var object1 = JSON.parse('{"rollno":101, "name":"Mayank", "age":20}');

So in the above code you can get the rollno by accessing the object1所以在上面的代码中你可以通过访问object1来获取rollno

console.log(object1.rollno);控制台日志(object1.rollno);

It would product 101 in the console.它会在控制台中产生 101。

Take your JSON string and run it through JSON.parse().获取您的 JSON 字符串并通过 JSON.parse() 运行它。

Please read https://www.geeksforgeeks.org/converting-json-text-to-javascript-object/请阅读https://www.geeksforgeeks.org/converting-json-text-to-javascript-object/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM