【发布时间】:2017-10-11 05:19:50
【问题描述】:
我有一个看起来像这样的响应对象 -
{
location: '{country:Poland,latitude:50.0575,longitude:19.9802}',
ip: '83.26.234.177',
name: 'John Doe'
}
我正在尝试这样读取国家/地区名称 -
data.forEach(function(datapoint) {
dataObject.ip = datapoint.ip;
var locationObject = datapoint.location; // also tried JSON.parse
console.log(locationObject); //{country:Poland,latitude:50.0575,longitude:19.9802}
console.log(locationObject.country); // undefined
console.log(locationObject.latitude); //undefined
console.log(locationObject.longitude); //undefined
}
获取undefined。
【问题讨论】:
-
您的位置属性是字符串。
-
是的,不认为这是有效的 json
-
location值不是有效的 JSON 字符串,有效的 JSON 字符串是{"country":"Poland","latitude":50.0575,"longitude":19.9802} -
datapoint.location是一个字符串。它没有任何属性。 -
修复给您无效响应的服务器。如果不是您的,请提交错误报告。
标签: javascript json node.js object