I appreciate there are many posts on this, and much on google, but I’m struggling to understand how to break up a JSON stream, and access the data.
I have a simple JSON response:
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4211908, 37.7564513]},
"properties": {
"id": "2950648771574984913",
"accuracyInMeters": 80,
"timeStamp": 1309323032,
"reverseGeocode": "San Francisco, CA, USA",
"photoUrl": "https://www.google.com/latitude/apps/badge/api?type=photo&photo=uuRL2jABAAA.9fWeRzNpS-tdX0cqHxxclg.7zdBNW-Rb634EIkOgyO8sw",
"photoWidth": 96,
"photoHeight": 96,
"placardUrl": "https://www.google.com/latitude/apps/badge/api?type=photo_placard&photo=uuRL2jABAAA.9fWeRzNpS-tdX0cqHxxclg.7zdBNW-Rb634EIkOgyO8sw&moving=true&stale=true&lod=1&format=png",
"placardWidth": 56,
"placardHeight": 59
}
}
]
}
I’m trying to get access to all of the data in it, such as:
The two coordinates.
The reverseGeocode.
etc.
I’ve built a function like this:
function findTristan(){
var FindUrl = "/proxy.php";
var tristanData = $.getJSON(FindUrl,function(json){});
// this is the part I have failed to get right.
alert(tristanData.coordinates);
}
Due to the asynchronous nature of AJAX you need to manipulate the data only inside the success callback as that’s the only place where this data is available. The
$.getJSONfunction returns immediately and it doesn’t return the result of the AJAX request. So the anonymous callback you left empty in your code should be used: