Saturday, 15 August 2015

javascript - How to get the value from JSON in JS -


I am trying to get the "formatted_address" value from this file. I am new to this and found documentation quite confusing. I now have the following code where the variable "location" is generated as the URL above.

  $ GetJSON (location, function (data) {stad = data.results.formatted_address; console.log (stad);});  

How do I get it?

result is an array, so you can access it as a is required. Your example is given with only one item, you can access it directly by index:

  var stad = data.results [0] .formatted_address; // = "'s-Hertogenbosch, Netherlands"  

If there are many items in the array, you will need a loop by them:

 for  (Var i = 0; i & lt; data .results.length; i ++) {var stad = data.results [i] .formatted_address; // Do something with the value for each iteration here ...}  

No comments:

Post a Comment