简体   繁体   中英

Creating Complex JSON Object using JavaScript Code

I am bit new to JSON world. And I have to use JavaScript to create following type of JSON structure. 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.

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

{
  "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 . With bracket notation, you can use any string like "L0b12leL-Ar9GYKoAAAC" .

Also note that in JSON , anything works as a property name too, as long as it is put in quotes. {"L0b12leL-Ar9GYKoAC":true} is as valid as {"💖":true} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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