简体   繁体   English

使用 JavaScript 代码创建复杂的 JSON 对象

[英]Creating Complex JSON Object using JavaScript Code

I am bit new to JSON world.我对 JSON 世界有点陌生。 And I have to use JavaScript to create following type of JSON structure.我必须使用 JavaScript 来创建以下类型的 JSON 结构。 Not sure how to achieve this.不知道如何实现这一目标。 Tried with following code, but unable to add second element("12101") as well as people to JSON Structure is where I am struggling.尝试使用以下代码,但无法将第二个元素(“12101”)以及人员添加到 JSON 结构中,这是我苦苦挣扎的地方。

var chat = {};
chat = {"101":{}};
chat["101"].people= {};
chat["101"].people = {"L0b12leL-Ar9GYKoAAAC":{}};
chat["101"].people.L0b12leL-Ar9GYKoAAAC = {"name":"vikram@qech.com"};
chat["101"].room= {};

JSON structure to achieve JSON结构实现

{
  "101": {
    "people": {
      "L0b12leL-Ar9GYKoAAAC": {
        "name": "vikram@qtech.com",
        "inroom": "f787f316-6424-491b-b779-cfc396f0f8a1",
        "owns": "f787f316-6424-491b-b779-cfc396f0f8a1",
        "countrycode": "in",
        "device": "desktop",
        "roomname": "R1"
      },
      "qKCglYWI1hRhZUZCAAAD": {
        "name": "Ishim",
        "inroom": "2e52905d-951c-4990-b9b7-2f3fc0602922",
        "owns": "2e52905d-951c-4990-b9b7-2f3fc0602922",
        "roomname": "Ra"
      }
    },
    "room": {
      "f787f316-6424-491b-b779-cfc396f0f8a1": {
        "name": "R1",
        "id": "f787f316-6424-491b-b779-cfc396f0f8a1",
        "owner": "L0b12leL-Ar9GYKoAAAC",
        "people": [
          "L0b12leL-Ar9GYKoAAAC"
        ],
        "status": "available"
      },
      "2e52905d-951c-4990-b9b7-2f3fc0602922": {
        "name": "Ra",
        "id": "2e52905d-951c-4990-b9b7-2f3fc0602922",
        "owner": "qKCglYWI1hRhZUZCAAAD",
        "people": [
          "qKCglYWI1hRhZUZCAAAD"
        ],
        "status": "available"
      }
    }
  },
  "12101": {
    "people": {
      "K-Ar9GYKoAAAC": {
        "name": "Rahul.com",
        "inroom": "f787f316-6424-491b-b779-cfc396f0f8a1",
        "owns": "f787f316-6424-491b-b779-cfc396f0f8a1",
        "countrycode": "in",
        "device": "desktop",
        "roomname": "R1"
      },
      "I1hRhZUZCAAAD": {
        "name": "Vipul",
        "inroom": "2e52905d-951c-4990-b9b7-2f3fc0602922",
        "owns": "2e52905d-951c-4990-b9b7-2f3fc0602922",
        "roomname": "Ra"
      }
    },
    "room": {
      "b779-cfc396f0f8a1": {
        "name": "Rahul-R1",
        "id": "f787f316-6424-491b-b779-cfc396f0f8a1",
        "owner": "L0b12leL-Ar9GYKoAAAC",
        "people": [
          "L0b12leL-Ar9GYKoAAAC"
        ],
        "status": "available"
      },
      "b9b7-2f3fc0602922": {
        "name": "Vipul-Room1",
        "id": "2e52905d-951c-4990-b9b7-2f3fc0602922",
        "owner": "qKCglYWI1hRhZUZCAAAD",
        "people": [
          "qKCglYWI1hRhZUZCAAAD"
        ],
        "status": "available"
      }
    }
  }
}

This is invalid because the property name contains dashes.这是无效的,因为属性名称包含破折号。

chat["101"].people.L0b12leL-Ar9GYKoAAAC = {"name":"vikram@qech.com"};

To access it correctly, put it in quotes要正确访问它,请将其放在引号中

chat["101"].people["L0b12leL-Ar9GYKoAAAC"] = {"name":"vikram@qech.com"};

Use bracket notation as a property accessor like this:使用括号表示法作为属性访问器,如下所示:

chat["12101"].people = {};
chat["101"].people["L0b12leL-Ar9GYKoAAAC"] = {"name":"vikram@qech.com"};

With it, it's just a routine piece of work.有了它,这只是一件例行的工作。 It probably didn't work right away since dot notation property access requires a valid identifier name .它可能没有立即起作用,因为点符号属性访问需要有效的标识符 name With bracket notation, you can use any string like "L0b12leL-Ar9GYKoAAAC" .使用括号表示法,您可以使用任何字符串,例如"L0b12leL-Ar9GYKoAAAC"

Also note that in JSON , anything works as a property name too, as long as it is put in quotes.另请注意,在JSON 中,任何内容也可用作属性名称,只要将其放在引号中即可。 {"L0b12leL-Ar9GYKoAC":true} is as valid as {"💖":true} . {"L0b12leL-Ar9GYKoAC":true}{"💖":true}一样有效

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

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