简体   繁体   English

将json对象追加到json对象的现有数组

[英]append json object to existing array of json object

I am trying to append new json object in existing json array of object. 我试图在对象的现有json数组中追加新的json对象。 I am new in json. 我是json的新手。 so please help me. 所以请你帮帮我

existing json : 现有的json:

{
   "cluster":[
      {
         "path":"home/Nik",
         "password":"welcome",
         "isQueen":"true",
         "host":"192.168.11.248",
         "isQueenWorker":"true",
         "user":"Nik"
      }
   ]
}

new json : 新的json:

{
   "path":"home\/Nik",
   "password":"welcome",
   "isQueen":"true",
   "host":"192.168.11.248",
   "isQueenWorker":"true",
   "user":"Nik"
}

I want to add new json to existing json array. 我想将新的json添加到现有的json数组。

You may use it like below, you need to use push command to push an object inside an array. 您可以像下面那样使用它,您需要使用push命令将对象推入数组中。

var myObj = {
   "cluster":[
      {
         "path":"home/Nik",
         "password":"welcome",
         "isQueen":"true",
         "host":"192.168.11.248",
         "isQueenWorker":"true",
         "user":"Nik"
      }
   ]
};
var x = {
   "path":"home\/Nik",
   "password":"welcome",
   "isQueen":"true",
   "host":"192.168.11.248",
   "isQueenWorker":"true",
   "user":"Nik"
};
alert(JSON.stringify(myObj))
var newArr = myObj.cluster; 
newArr.push(x) //pushing object x in newArr. similarly you can add multiple objects in to it
var myJSON = JSON.stringify(newArr);
alert(myJSON)

you can directly append it 您可以直接附加

eg. 例如。 first json 第一个json

{"cluster":[{"path":"home/Nik","password":"welcome","isQueen":"true","host":"192.168.11.248","isQueenWorker":"true","user":"Nik"}]}

int i= cluster.length(); int i = cluster.length();

cluster[i]={"path":"home/Nik","password":"welcome","isQueen":"true","host":"192.168.11.248","isQueenWorker":"true","user":"Nik"}

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

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