Saturday 20 May 2017

How to parse a string into JSON that (I think) contains arrays

Given a string that is appears to be in valid JSON format (received from an api call), how can I parse that string such that I can get access to

The value of JSON.parse(data) comes back as JSON that contains [Object], which is not helpful to me.
I'm trying to get the lat and lng out of data that is sent to me as a string that I think can be turned into JSON.
I know I could just mess around with data as a string, but I want to use it as JSON.

{ results: 
   [ { address_components: [Object],
       formatted_address: 'Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
       geometry: [Object],
       place_id: 'ChIJxQvW8wK6j4AR3ukttGy3w2s',
       types: [Object] } ],
  status: 'OK' }

Here is the value of data:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Google Building 41",
               "short_name" : "Google Bldg 41",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Google Bldg 41, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 37.4228642,
                  "lng" : -122.0851557
               },
               "southwest" : {
                  "lat" : 37.4221145,
                  "lng" : -122.0859841
               }
            },
            "location" : {
               "lat" : 37.4224082,
               "lng" : -122.0856086
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238383302915,
                  "lng" : -122.0842209197085
               },
               "southwest" : {
                  "lat" : 37.4211403697085,
                  "lng" : -122.0869188802915
               }
            }
         },
         "place_id" : "ChIJxQvW8wK6j4AR3ukttGy3w2s",
         "types" : [ "premise" ]
      }
   ],
   "status" : "OK"
}



via Glen Pierce

No comments:

Post a Comment