我无法在JSON中推送对象,但是console.log()可以正常工作。 console.log()显示添加了一个对象,但是没有出现json string?
var results = [{
"id": 0,
"name": "Green",
"category": "project",
"year": 2008,
"description": "my description",
"comments": [{
"rating": 5,
"comment": "Perfect",
"author": "Sandra",
"date": "2012-10-16T17:57:28.556094Z"
}, {
"rating": 4,
"comment": "Cool",
"author": "Milena",
"date": "2014-09-05T17:57:28.556094Z"
}, {
"rating": 3,
"comment": "No bad",
"author": "Milan",
"date": "2015-02-13T17:57:28.556094Z"
}, {
"rating": 4,
"comment": "Good job",
"author": "Toma",
"date": "2013-12-02T17:57:28.556094Z"
}, {
"rating": 2,
"comment": "OK",
"author": "Aleksandar",
"date": "2011-12-02T17:57:28.556094Z"
}]
}];
document.getElementById('show').innerHTML = results[0].comments[1].author;
document.getElementById("sendComment").onclick = function() {
var hg = {
"rating": 1,
"comment": "Hello",
"author": "X",
"date": "2014-09"
}
results[0].comments.push(hg);
console.log(results[0].comments);
}

<div id='show'></div>
<button id='sendComment'>Send</button>
&#13;
答案 0 :(得分:0)
results [0] .comments是对象数组,而不是JSON字符串。一切正常。新对象被添加到数组中。