在JSON对象上向数组添加新项

时间:2018-05-31 07:51:07

标签: javascript json

我有JSON对象,如下所示;我想在我的Json中添加新的Label 5和Label 6 Element。我使用了这个命令,但是我收到了这个错误:找不到对象[object Object]中的函数push

我从这里找到了一个例子; Adding a new array element to a JSON object

我在我的应用程序上尝试了这个例子,它正在工作,但我的json无效。

var jsonStr = 'Json bla bla. you can see at below';
    var obj 	= JSON.parse(jsonStr);
    obj['reservationNetworks'].push({"type": "complex","componentTypeId": "nw.service","componentId": null,"classId": "Reservation.Network","typeFilter": null,"values": {"entries": [{"key": "networkPath","value": {"type": "entityRef","classId": "Network","id": "6afec529-7fbc-45b7-8ac5-6abf944946ce","componentId": null,"label": "Label 5"}},{"key": "networkProfile","value": {"type": "entityRef","classId": "networkProfile","id": "187014d4-62f6-434c-a4da-c3e262d25ed4","componentId": null,"label": "Label 6"}}]}});
    jsonStr = JSON.stringify(obj);

    {
                "key": "reservationNetworks",
                "value": {
                    "type": "multiple",
                    "elementTypeId": "COMPLEX",
                    "items": [
                        {
                            "type": "complex",
                            "componentTypeId": "nw.service",
                            "componentId": null,
                            "classId": "Reservation.Network",
                            "typeFilter": null,
                            "values": {
                                "entries": [
                                    {
                                        "key": "networkPath",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "Network",
                                            "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                                            "componentId": null,
                                            "label": "Label 1"
                                        }
                                    },
                                    {
                                        "key": "networkProfile",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "networkProfile",
                                            "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                                            "componentId": null,
                                            "label": "Label 2"
                                        }
                                    }
                                ]
                            }
                        },
{
                            "type": "complex",
                            "componentTypeId": "nw.service",
                            "componentId": null,
                            "classId": "Reservation.Network",
                            "typeFilter": null,
                            "values": {
                                "entries": [
                                    {
                                        "key": "networkPath",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "Network",
                                            "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                                            "componentId": null,
                                            "label": "Label 3"
                                        }
                                    },
                                    {
                                        "key": "networkProfile",
                                        "value": {
                                            "type": "entityRef",
                                            "classId": "networkProfile",
                                            "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                                            "componentId": null,
                                            "label": "Label 4"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }

1 个答案:

答案 0 :(得分:1)

这有点难以辨别,但我想你想要将你想要推送的数据添加到items数组中。您可以通过指定正确的“路径”来实现:

obj.value.items.push(...)

var obj = JSON.parse(jsonStr);
obj.value.items.push({
  "type": "complex",
  "componentTypeId": "nw.service",
  "componentId": null,
  "classId": "Reservation.Network",
  "typeFilter": null,
  "values": {
    "entries": [{
      "key": "networkPath",
      "value": {
        "type": "entityRef",
        "classId": "Network",
        "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
        "componentId": null,
        "label": "Label 5"
      }
    }, {
      "key": "networkProfile",
      "value": {
        "type": "entityRef",
        "classId": "networkProfile",
        "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
        "componentId": null,
        "label": "Label 6"
      }
    }]
  }
});
console.log( JSON.stringify(obj) );
<script>
  const jsonStr = `{
    "key": "reservationNetworks",
    "value": {
      "type": "multiple",
      "elementTypeId": "COMPLEX",
      "items": [{
          "type": "complex",
          "componentTypeId": "nw.service",
          "componentId": null,
          "classId": "Reservation.Network",
          "typeFilter": null,
          "values": {
            "entries": [{
                "key": "networkPath",
                "value": {
                  "type": "entityRef",
                  "classId": "Network",
                  "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                  "componentId": null,
                  "label": "Label 1"
                }
              },
              {
                "key": "networkProfile",
                "value": {
                  "type": "entityRef",
                  "classId": "networkProfile",
                  "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                  "componentId": null,
                  "label": "Label 2"
                }
              }
            ]
          }
        },
        {
          "type": "complex",
          "componentTypeId": "nw.service",
          "componentId": null,
          "classId": "Reservation.Network",
          "typeFilter": null,
          "values": {
            "entries": [{
                "key": "networkPath",
                "value": {
                  "type": "entityRef",
                  "classId": "Network",
                  "id": "6afec529-7fbc-45b7-8ac5-6abf944946ce",
                  "componentId": null,
                  "label": "Label 3"
                }
              },
              {
                "key": "networkProfile",
                "value": {
                  "type": "entityRef",
                  "classId": "networkProfile",
                  "id": "187014d4-62f6-434c-a4da-c3e262d25ed4",
                  "componentId": null,
                  "label": "Label 4"
                }
              }
            ]
          }
        }
      ]
    }
  }`;
</script>