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

维克拉姆

我对JSON世界有点陌生。而且我必须使用JavaScript创建以下类型的JSON结构。不知道如何实现这一目标。我尝试使用以下代码,但无法在JSON结构中添加第二个元素(“ 12101”)以及其他人,这是我一直在努力的地方。

var chat = {};
chat = {"101":{}};
chat["101"].people= {};
chat["101"].people = {"L0b12leL-Ar9GYKoAAAC":{}};
chat["101"].people.L0b12leL-Ar9GYKoAAAC = {"name":"[email protected]"};
chat["101"].room= {};

JSON结构实现

{
  "101": {
    "people": {
      "L0b12leL-Ar9GYKoAAAC": {
        "name": "[email protected]",
        "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"
      }
    }
  }
}
卡塔利娜岛

这是无效的,因为属性名称包含破折号。

chat["101"].people.L0b12leL-Ar9GYKoAAAC = {"name":"[email protected]"};

要正确访问它,请用引号引起来

chat["101"].people["L0b12leL-Ar9GYKoAAAC"] = {"name":"[email protected]"};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档