我知道這聽起來很荒謬,但我一直在試圖讀取從谷歌地圖API的這些JSON數據,但似乎都沒有它的工作原理我如何閱讀這些json數據?
下面的代碼
app.get('/distance', function(req, res, next) {
request('https://maps.googleapis.com/maps/api/distancematrix/json?origins=Seattle&destinations=San+Francisco&key=' + config.GOOGLE_DISTANCE_API, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
});
});
體將返回該數據
{
"destination_addresses" : [ "New York, NY, USA" ],
"origin_addresses" : [ "Washington, DC, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "225 mi",
"value" : 361715
},
"duration" : {
"text" : "3 hours 49 mins",
"value" : 13725
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
當我嘗試CONSOLE.LOG body.destination_addresses
和以及body.rows
,這一切顯示了不確定的。我如何訪問它?
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – Quentin