taevanbat
taevanbat

Reputation: 435

Accessing value in JSON object via Javascript

I am getting a JSON object via GET using Javascript. This is a result of reverse geocoding in Google Earth (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"
}

On the instructions, they tell us to parse the JSON like so (using JS):

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

But, it's not working. I'm assuming that myAddress is an array that has already been defined before and has nothing in it and that myJSONResult is the variable you used to store the response from the HTTP request. How can I get the formatted_address string?

Upvotes: 0

Views: 553

Answers (2)

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

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);

                }

            }

        }

Upvotes: 0

T. Junghans
T. Junghans

Reputation: 11683

As described by google the response needs to be parsed

Note that these results generally need to be parsed if you wish to extract values from the results. Parsing JSON is relatively easy.

Modern browser can convert strings to json with https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse otherwise there are libraries such as https://github.com/douglascrockford/JSON-js or jQuery with parseJSON.

Upvotes: 1

Related Questions