簡體   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