-1

Hi i am new to json and have some problems in parsing a JSON file with jQuery.

json file is here - http://maps.googleapis.com/maps/api/geocode/json?latlng=27.88,78.08&sensor=false

and i am parsing it like this

var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false";
$.getJSON(url, function(data) {
    console.log("insidegetjson");
    console.log(data);
    var addr = data.results[0].formatted_address[0];
});

here i want to access first "formatted_address" part of the JSON. I know i am making a mistake here (i want "formatted_address" : "Achal Taal, Aligarh, Uttar Pradesh 202001, India",)

var addr = data.results[0].formatted_address[0]; 

can you please replace this sentence with correct sentence...thanks

Sergio
  • 28,539
  • 11
  • 85
  • 132
Wanna_be_Coder
  • 29
  • 1
  • 1
  • 10

1 Answers1

2

Remove the last [0] :

var addr = data.results[0].formatted_address; 

data.results[0].formatted_address isn't an array but a string.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758