I want to extract data from a Json URL and organize it into an array structured as shown below:
var locations = [ ['Bondi Beach', -30.890542, 151.274856], ['Coogee Beach', -33.923036, 151.259052], ['Cronulla Beach', -34.028249, 151.157507], ['Manly Beach', -33.80010128657071, 151.28747820854187], ['Maroubra Beach', -33.950198, 151.259302] ]
Here is the snippet of code I am working with... can you spot any errors?
Updated:
var locations = new Array();
$(document).ready(function(){
$.getJSON('c.js', function(jsonData) {
$.each(jsonData, function(key, value) {
$('ul').append('<li id="' + key + '">' + value.city + ' : ' + value.latitude + ' , ' + value.longitude +'</li>');
locations[key] = [value.city, value.latitude, value.longitude];
});
});
window.alert (locations);
});
Solved
var locations = new Array();
$(document).ready(function(){
$.getJSON('http://www.xxxxxx/index.php', function(jsonData) {
$.each(jsonData, function(key, value) {
$('ul').append('<li id="' + key + '">' + value.city + ' : ' + value.latitude + ' , ' + value.longitude +'</li>');
locations.push([value.city, value.latitude, value.longitude]);
});
But I discovered that all my code needed to be contained within this function for it to work properly.