2011-11-09 115 views
0

如果我有這樣的結構我怎樣才能元素加入到這個對象

var data = 
{ 
    "people": [ 
     { "name" : "John", "id" : 1 }, 
     { "name" : "Marc", "id" : 2 } 
    ] 
} 

我想更多的元素添加到這一點,在JavaScript中,特別是在jQuery來然後把它像這樣

var dataString = JSON.stringify(data); 
$.post('some.php', { data: dataString}, showResult, "text"); 

回答

3
var data = 
{ 
    "people": [ 
     { "name" : "John", "id" : 1 }, 
     { "name" : "Marc", "id" : 2 } 
    ] 
} 
data["people"].push({"name": "foo", "id": 3}); 

console.log(data); 

http://jsfiddle.net/RK4bx/

相關問題