2017-07-14 175 views
0

我正在從服務器接收數據,並試圖從中獲取特定值。從JSON中提取數據返回undefined

數據在名爲body的變量中接收。下面是我做了什麼:

console.log(body); 
console.log(body._id); 
console.log(body["_id"]); 

這裏是輸出:

{"__v":0,"name":"asd","address":"asd","coords":[42,52],"dateTime":"2017-12-31T23:59","_id":"5968c7e244d4c66abdb3370b","comments":[],"tags":["asd"]} 

undefined 

undefined 

的文件肯定有一個叫鍵「_id」爲什麼我不能檢索它的價值呢?

+1

你需要先用'JSON.parse'解析JSON。您正嘗試訪問字符串的_id屬性 – tom10271

回答