简体   繁体   中英

Parse deep JSON object in angularjs

I am using google maps API, and getting the response from google. Just need to get distance "text" value, eg 57.6 mi and duration "text" value where it is "1 hour and 1 min" from JSON that gets returned.

 { "destination_addresses" : [ "blah blah" ], "origin_addresses" : [ "some address" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "57.6 mi", "value" : 92652 }, "duration" : { "text" : "1 hour 1 min", "value" : 3664 }, "status" : "OK" } ] } ], "status" : "OK" }

I have tried to parse it out by using dot notation and key-value pairs, but the deepest I could get is getting "rows" - which returned me object Object.

@Claies beat me to it, you can get the distance using data.rows[0].elements[0].distance.text

 var data = { "destination_addresses": ["blah blah"], "origin_addresses": ["some address"], "rows": [{ "elements": [{ "distance": { "text": "57.6 mi", "value": 92652 }, "duration": { "text": "1 hour 1 min", "value": 3664 }, "status": "OK" }] }], "status": "OK" } document.write(data.rows[0].elements[0].distance.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM