如何将数据添加到json的子节点

时间:2015-03-27 09:10:36

标签: javascript jquery json

我想将数据{"address" : "http://ebay.com"}添加到此json数据的第二个子节点,即数据"api" : "Ebay"的括号中。

我怎么能用jQuery做到这一点?

{"Amazon":[
    {"price":"19,799.00","delivery":"FREE","api":"Amazon"},
    {"price":"29,999","delivery":"FREE","api":"Ebay"}
  ]
}

1 个答案:

答案 0 :(得分:2)

尝试,

var obj = {"Amazon":[ {"price":"19,799.00","delivery":"FREE","api":"Amazon"},{"price":"29,999","delivery":"FREE","api":"Ebay"} ] };
obj.Amazon[1].address = "http://ebay.com";

或使用$.merge(obj1,obj2)功能

var obj = {"Amazon":[ {"price":"19,799.00","delivery":"FREE","api":"Amazon"},{"price":"29,999","delivery":"FREE","api":"Ebay"} ] };

$.merge(obj.Amazon[1], {
    address: "http://ebay.com"
});

DEMO