2

我有一个谷歌 Mpa Json 网址 http://maps.googleapis.com/maps/api/distancematrix/json?&origins=2020%20E%20main%20ST&destinations=100%20E%20Main%20St%20,Edmond,Ok&mode=driving&sensor =假

这给了我

{
   "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

4

1 回答 1

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] 来获取每个数组中的第一项。

于 2013-01-30T18:06:08.820 回答