user2890734
user2890734

Reputation: 1

Use jQuery to iterate through JSON array [WP coordinates created by Advanced Custom Fields and JSON API]

I have a custom field, called coordinates, creating a Google Map inside a Wordpress post. (I am using two plugins to do this: Advanced Custom Fields and JSON API.) I end up with a JSON array like the following:

{\"address\":\"425 Northern Ave Thunder Bay, ON\",\"lat\":48.3959003,\"lng\":-89.2453188,\"zoom\":16}

In my script, I access the array like so:

var thisMap = JSON.parse(val.custom_fields.coordinates);

The following returns all the keys inside the array:

    for ( val in thisMap ) {
        if (thisMap.hasOwnProperty(val)) {
        console.log(val);
    }

I get: address lng lat zoom

How can I target just the latitude and/or longitude? (i.e. I would like to be able to target just "48.3959003" or "-89.2453188", so I can use them to create a Google Map.)

Upvotes: 0

Views: 178

Answers (2)

cr0ss
cr0ss

Reputation: 877

Try this DEMO

It returns a javascript object, and you can access its properties directly

var val = "{\"address\":\"425 Northern Ave Thunder Bay, ON\",\"lat\":48.3959003,\"lng\":-89.2453188,\"zoom\":16}";    
var thisMap = JSON.parse(val);

console.log(thisMap);
console.log(thisMap.lat);
console.log(thisMap.lng);

Upvotes: 1

Adam Bubela
Adam Bubela

Reputation: 10063

Did you try this?

var obj = JSON.parse('{\"address\":\"425 Northern Ave Thunder Bay, ON\",\"lat\":48.3959003,\"lng\":-89.2453188,\"zoom\":16}'); 
alert('latitude:'+obj.lat);

Upvotes: 1

Related Questions