我正在尝试将新的 json 对象附加到现有的 json 对象数组中。我是 json 新手。所以请帮助我。
现有 json:
{
"cluster":[
{
"path":"home/Nik",
"password":"welcome",
"isQueen":"true",
"host":"192.168.11.248",
"isQueenWorker":"true",
"user":"Nik"
}
]
}
新的 json :
{
"path":"home\/Nik",
"password":"welcome",
"isQueen":"true",
"host":"192.168.11.248",
"isQueenWorker":"true",
"user":"Nik"
}
我想将新的 json 添加到现有的 json 数组中。
最佳答案
你可以像下面这样使用它,你需要使用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)
关于java - 将 json 对象追加到现有 json 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50738292/