给了我
{
"destination_addresses" : [ "100 East Main Street, Edmond, OK 73034, USA" ],
"origin_addresses" : [ "2020 East Main Street, Lamar, AR 72846, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "410 km",
"value" : 409567
},
"duration" : {
"text" : "3 hours 49 mins",
"value" : 13759
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我正在尝试使用
解析它 function callGoogle(inputOrigin, inputDst, inputMode)
{
$.getJSON('http://maps.googleapis.com/maps/api/distancematrix/json?', { origins: inputOrigin, destinations: inputDst, mode: inputMode, sensor: false }, function (data) {
$.each(data, function (i, item) {
console.log(item.rows.elements.distance.text);
});
});
}
但是我得到了错误。如何使用jquery
从这个json获得97.8或97766答案 0 :(得分:4)
你的意思是这样吗?
var data = {
"destination_addresses": ["1000 Chopper Circle, Denver, CO 80204, USA"],
"origin_addresses": [
"2121 East Mulberry Street, Fort Collins Nursery, Fort Collins, CO 80524, USA"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "97.8 km",
"value": 97766
},
"duration": {
"text": "57 mins",
"value": 3449
},
"status": "OK"
}
]
}
],
"status": "OK"
};
alert(data.rows[0].elements[0].distance.value);
注意,数据有两个数组,我使用[0]来获取每个数组中的第一个项目。