Monday, March 19, 2018

javascript: 打印json

如果用console.log輸出json,瀏覽器會自動分析成obj,無法以字串形式儲存,需要用JSON.stringify(obj);才可以將json以string 來輸出。

如下︰
obj = [{id:1}, {id:2}]
<< console.log(obj)
>> "[object Object],[object Object]"

<< console.log(JSON.stringify(obj));
>> "[{id:1}, {id:2}]"

No comments: