从JSON提取数据返回未定义的数据

从JSON提取数据返回未定义的数据,json,Json,我从服务器接收数据,并试图从中获取特定值 数据在称为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"

我从服务器接收数据,并试图从中获取特定值

数据在称为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

文件肯定有一个名为“\u id”的键,所以为什么我不能检索它的值?

您需要首先将JSON解析为一个对象

var parsedBody = JSON.parse(body);
console.log(parsedBody);
console.log(parsedBody._id);

首先需要使用JSON.parse解析JSON。您正在尝试访问字符串的_id属性,不需要解析