1

我有这样的json:

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

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

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

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

4

3 回答 3

2

尝试

adlist.81.push({name: "Douglas Adams", id: "comedy"});
于 2013-10-29T06:24:57.350 回答
1

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

adlist.json["81"][adlist.json["81"].length]  ={name: "Douglas Adams", id: "comedy"};
于 2013-10-29T06:30:47.880 回答
0

它对我有用:)

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);
于 2013-10-29T06:47:09.877 回答