0

我正在使用 Javascript 通过 GET 获取 JSON 对象。这是 Google 地球 ( https://developers.google.com/maps/documentation/geocoding/#GeocodingRequests ) 中反向地理编码的结果。

{
   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "1",
           "short_name" : "1",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Church Street",
           "short_name" : "Church St",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Mid-Cambridge",
           "short_name" : "Mid-Cambridge",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Cambridge",
           "short_name" : "Cambridge",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Cambridge",
           "short_name" : "Cambridge",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Middlesex",
           "short_name" : "Middlesex",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Massachusetts",
           "short_name" : "MA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "02138",
           "short_name" : "02138",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "1 Church Street, Cambridge, MA 02138, USA",
     "geometry" : {
        "location" : {
           "lat" : 42.37440120,
           "lng" : -71.11850609999999
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 42.37575018029150,
              "lng" : -71.11715711970849
           },
           "southwest" : {
              "lat" : 42.37305221970851,
              "lng" : -71.11985508029150
           }
        }
     },
     "postcode_localities" : [],
     "types" : [ "street_address" ]
  }
   ],
   "status" : "OK"
}

在说明中,他们告诉我们像这样解析 JSON(使用 JS):

for (i = 0; i < myJSONResult.results.length; i++) {
  myAddress[i] = myJSONResult.results[i].formatted_address;
}

但是,它不起作用。我假设 myAddress 是一个之前已经定义过的数组,其中没有任何内容,并且 myJSONResult 是您用于存储来自 HTTP 请求的响应的变量。如何获取 formatted_address 字符串?

4

2 回答 2

1

正如谷歌所描述的,需要解析响应

请注意,如果您希望从结果中提取值,通常需要解析这些结果。解析 JSON 相对容易。

现代浏览器可以使用https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse将字符串转换为 json,否则有诸如https://github.com/douglascrockford/JSON之类的库-js或带有parseJSON的 jQuery 。

于 2013-05-12T11:43:23.430 回答
0

jsfiddle.net

var resultsData = JSONOBJECT;

function ProcessResults(resultsData) {

            if (resultsData != null) {                               

                if (resultsData.status == "OK") {

                    var address = "", city = "", state = "", zip = "", country = "", formattedAddress = "";
                    var lat;
                    var lng;

                    for (var i = 0; i < resultsData.results[0].address_components.length; i++) {
                        var addr = resultsData.results[0].address_components[i];
                        // check if this entry in address_components has a type of country
                        if (addr.types[0] == 'country')
                            country = addr.long_name;
                        else if (addr.types[0] == 'street_address') // address 1
                            address = address + addr.long_name;
                        else if (addr.types[0] == 'establishment')
                            address = address + addr.long_name;
                        else if (addr.types[0] == 'route')  // address 2
                            address = address + addr.long_name;
                        else if (addr.types[0] == 'postal_code')       // Zip
                            zip = addr.short_name;
                        else if (addr.types[0] == ['administrative_area_level_1'])       // State
                            state = addr.long_name;
                        else if (addr.types[0] == ['locality'])       // City
                            city = addr.long_name;
                    }


                    if (resultsData.results[0].formatted_address != null) {
                        formattedAddress = resultsData.results[0].formatted_address;
                    }



                    var location = resultsData.results[0].geometry.location;



                    lat = location.lat;
                    lng = location.lng;

                    alert('City: '+ city + '\n' + 'State: '+ state + '\n' + 'Zip: '+ zip + '\n' + 'Formatted Address: '+ formattedAddress + '\n' + 'Lat: '+ lat + '\n' + 'Lng: '+ lng);

                }

            }

        }
于 2013-05-12T12:11:18.920 回答