通过Javascript访问JSON对象中的值

时间:2013-05-12 11:23:46

标签: javascript json parsing google-maps

我使用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字符串?

2 个答案:

答案 0 :(得分:1)

正如所描述的by google,需要解析响应

  

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

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

答案 1 :(得分: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);

                }

            }

        }