繁体   English   中英

如何将元素插入json数组

[英]how to insert the element into the json array

我有这样的json:

{
    json{
        "81":[
            {
            "name":"",
            "id":""
            },
            {
            "name":"",
            "id":""
            }
        ]
    }
}

我动态地得到这个json,我将这个json存储在adlist中。 我想将元素插入到第三个位置的数组中。 我试过这样的:

 var temp={"name":"","id":""};
 adlist.json[81].splice(2,0,temp);

但它没有正确添加字符串。

尝试

adlist.81.push({name: "Douglas Adams", id: "comedy"});

你也可以试试这个以获得更好的性能,

adlist.json["81"][adlist.json["81"].length]  ={name: "Douglas Adams", id: "comedy"};

它对我有用:)

Java脚本代码:

//your json data
var adlist = {json:{"81":[{"name":"first","id":"1"},{"name":"second","id":"2"}]}};

// your new json data
var temp={"name":"Third","id":"3"};

// insert your new json data in 3rd position
adlist.json["81"].push(temp);

// check with console
console.log(adlist);

暂无
暂无

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

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