0

I have the following code:

var addressInfo = [];
for(var x in address_components_root){
    addressInfo.push(address_components_root[x].long_name+"|||"+address_components_root[x].types[0]);
}


for(var d in addressInfo) {
    addressI = addressInfo[d].split("|||");
    if (addressI[1]=="street_number") {
        window.alert("street");
    }
}

address_components_root is a JSON encoded response as found at http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

How would I parse this json correctly like for example using eval so I don;t need to manually assign all the data?

3
  • JSON = Javascript Object Notation Commented Feb 3, 2012 at 12:20
  • @Sandro: And what do you want to say with that? Commented Feb 3, 2012 at 12:23
  • I just wanted to point that out. It is close to javascript. Commented Feb 3, 2012 at 14:05

1 Answer 1

3

In modern browsers you can use JSON.parse([json string]);. In older browsers you need a JSON library: see this page - use the json2.js file.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I receive the error : Uncaught TypeError: Object function json_decode(str_json) - is this because the data I get from google is already an object? Sorry, bit of a JSON noob here
It works here with the link you provided. See jsfiddle.net/hE8xf for the right syntax. What's in json_decode?
My bad, was using googles api. Already returns a JSON object. What a fool. I was using the same function on a server side version of the script that fetched the data from the REST api and then the client side which fetched it from the JS api.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.