繁体   English   中英

我无法在 JSON 中推送对象,但 console.log() 有效

[英]I can not push an object in JSON, but console.log() works

我无法在 JSON 中推送对象,但 console.log() 可以工作。 console.log() 显示添加了一个对象,但是没有出现json字符串?

 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>

results[0].comments 是对象数组,而不是 JSON 字符串。 一切正常。 新对象被添加到数组中。

暂无
暂无

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

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