I have a basic question on JSON Jquery, and it seams I am sort of stuck some at a point of extracting array objects. My Code below is in javascript, and I just wanted some clarifications as what I might be doing wrong here.
<?php
$nor = $_SESSION["north"];
$sou = $_SESSION["south"];
$eas = $_SESSION["east"];
$wes = $_SESSION["west"];
session_destroy();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>jQuery JSON test</title>
</head>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
var north = "<?php echo $nor ?>";
var south = "<?php echo $sou ?>";
var east = "<?php echo $eas ?>";
var west = "<?php echo $wes ?>";
document.write(north,south,east,west);
$(document).ready(function(){
alert("begin loop");
$.getJSON('http://api.geonames.org/earthquakesJSON?north=' + north + ' &south=' + south + '&east=' + east + '&west=' + west +'&callback=?',
function(data){
alert(data.earthquakes);
});
})
</script>
</body>
</html>
So when I use alert(data.earthquakes); I get undefined operation, which is fine but I know here I am getting a response back. However the architecture of the JSON is as following:
{"earthquakes": [
{"eqid":"2007hear","magnitude":8.4,"lng":101.3815,"src":"us","datetime":"2007-09-12 09:10:26","depth":30,"lat":-4.5172},
{"eqid":"2007aqbk","magnitude":8,"lng":156.9567,"src":"us","datetime":"2007-04-01 18:39:56","depth":10,"lat":-8.4528},
{"eqid":"2007hec6","magnitude":7.8,"lng":100.9638,"src":"us","datetime":"2007-09-12 21:49:01","depth":10,"lat":-2.5265}
]}
So I have tried different ways of extracting information such as alert(data.earthquakes[1].eqid);
and alert(data.earthquakes.eqid[1]);
, however I am not getting the dedicated array as wanted.
Could someone direct me as
- how to get the desired architecture result appropriately and
- if I want to use array and for loop to extract all the elements into local array,
how to do that?
If your JSON object is defined as follows:
Try this:
This is a good point of reference: http://www.json.org/js.html