Reputation: 1
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
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
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