-2

Can someone give some example how to parse below object? All I need is one field.

I see lot of example with simple objects, not for complex.

{"results":[{"address_components":[{"long_name":"Nashville","short_name":"Nashville","types":["locality","political"]},{"long_name":"Davidson County","short_name":"Davidson County","types":["administrative_area_level_2","political"]},{"long_name":"Tennessee","short_name":"TN","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Nashville, TN, USA","geometry":{"bounds":{"northeast":{"lat":36.4054959,"lng":-86.5155879},"southwest":{"lat":35.9677851,"lng":-87.054903}},"location":{"lat":36.1626638,"lng":-86.7816016},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":36.4054959,"lng":-86.5155879},"southwest":{"lat":35.9677851,"lng":-87.054903}}},"place_id":"ChIJPZDrEzLsZIgRoNrpodC5P30","types":["locality","political"]}],"status":"OK"}

  • This is not "parsing", which is something different. Don't use that word. It is "accessing". It has nothing to do with Angular. It is plain old JavaScript. Your title says "parse and display"; your question mentions "parsing", but not "display". Is your problem that you want to "parse" it, or "display" it? Which field do you want? What problems are you having accessing it? –  Jun 25 '17 at 01:39
  • 2
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) –  Jun 25 '17 at 01:39

1 Answers1

0

Which field do you want ? Because your Object seems already ok. By example :

var test = {"results":[{"address_components":[{"long_name":"Nashville","short_name":"Nashville","types":["locality","political"]},{"long_name":"Davidson County","short_name":"Davidson County","types":["administrative_area_level_2","political"]},{"long_name":"Tennessee","short_name":"TN","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]}],"formatted_address":"Nashville, TN, USA","geometry":{"bounds":{"northeast":{"lat":36.4054959,"lng":-86.5155879},"southwest":{"lat":35.9677851,"lng":-87.054903}},"location":{"lat":36.1626638,"lng":-86.7816016},"location_type":"APPROXIMATE","viewport":{"northeast":{"lat":36.4054959,"lng":-86.5155879},"southwest":{"lat":35.9677851,"lng":-87.054903}}},"place_id":"ChIJPZDrEzLsZIgRoNrpodC5P30","types":["locality","political"]}],"status":"OK"};
test.results[0].place_id; // will give you "ChIJPZDrEzLsZIgRoNrpodC5P30"

If you want "southwest":{"lat":35.9677851,"lng":-87.054903}, you can use :

var southwestValues = test.results[0].geometry.bounds.southwest;
var lat = southwestValues.lat; // gives you 35.9677851
var long = southwestValues.lng; // gives you -87.054903

Is that the answer you were looking for?

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
vanessa
  • 419
  • 1
  • 4
  • 13