Google 映射和访问返回的对象
Google Maps and accessing returned Object
这几乎肯定是非常基础的东西,但我终究无法找出问题所在。我有一个数据对象从 Google Maps JS API
返回
{
"destination_addresses" : [ "12 Ansell Rd, London SW17, UK" ],
"origin_addresses" : [
"911 Alfreds Way, Barking IG11 0AX, UK",
"911 Alfreds Way, Barking IG11 0AX, UK",
"50 Buttesland St, Hoxton, London N1 6BY, UK"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "24.4 km",
"value" : 24411
},
"duration" : {
"text" : "55 mins",
"value" : 3297
},
"duration_in_traffic" : {
"text" : "58 mins",
"value" : 3454
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "24.4 km",
"value" : 24411
},
"duration" : {
"text" : "55 mins",
"value" : 3297
},
"duration_in_traffic" : {
"text" : "58 mins",
"value" : 3454
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "13.9 km",
"value" : 13905
},
"duration" : {
"text" : "48 mins",
"value" : 2909
},
"duration_in_traffic" : {
"text" : "45 mins",
"value" : 2717
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我正在使用 .get 和 require 的 https 模块检索它,它工作得非常好,然后我像这样打印数据...
https.get(`https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.5285,0.0847|51.5285,0.0847|51.5285,-0.0847&destinations=51.432622,-0.164496&departure_time=now&key=APIKEY`, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
//When all data is received
resp.on('end', () => {
console.log(data);
});
});
问题是当我尝试访问(甚至只是控制台日志)对象中的任何数据时,如
data.destination_addresses
它 returns 未定义。我认为这可能与异步问题有关,但我可以打印数据变量而不是单独打印它的属性是没有意义的
您需要先将其解析为 JSON
var obj = JSON.parse(data);
console.log(obj.destination_addresses);
这几乎肯定是非常基础的东西,但我终究无法找出问题所在。我有一个数据对象从 Google Maps JS API
返回 {
"destination_addresses" : [ "12 Ansell Rd, London SW17, UK" ],
"origin_addresses" : [
"911 Alfreds Way, Barking IG11 0AX, UK",
"911 Alfreds Way, Barking IG11 0AX, UK",
"50 Buttesland St, Hoxton, London N1 6BY, UK"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "24.4 km",
"value" : 24411
},
"duration" : {
"text" : "55 mins",
"value" : 3297
},
"duration_in_traffic" : {
"text" : "58 mins",
"value" : 3454
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "24.4 km",
"value" : 24411
},
"duration" : {
"text" : "55 mins",
"value" : 3297
},
"duration_in_traffic" : {
"text" : "58 mins",
"value" : 3454
},
"status" : "OK"
}
]
},
{
"elements" : [
{
"distance" : {
"text" : "13.9 km",
"value" : 13905
},
"duration" : {
"text" : "48 mins",
"value" : 2909
},
"duration_in_traffic" : {
"text" : "45 mins",
"value" : 2717
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我正在使用 .get 和 require 的 https 模块检索它,它工作得非常好,然后我像这样打印数据...
https.get(`https://maps.googleapis.com/maps/api/distancematrix/json?origins=51.5285,0.0847|51.5285,0.0847|51.5285,-0.0847&destinations=51.432622,-0.164496&departure_time=now&key=APIKEY`, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
//When all data is received
resp.on('end', () => {
console.log(data);
});
});
问题是当我尝试访问(甚至只是控制台日志)对象中的任何数据时,如
data.destination_addresses
它 returns 未定义。我认为这可能与异步问题有关,但我可以打印数据变量而不是单独打印它的属性是没有意义的
您需要先将其解析为 JSON
var obj = JSON.parse(data);
console.log(obj.destination_addresses);