JSON Jquery 数组类型
我有一个关于 JSON Jquery 的基本问题,看起来我在提取数组对象时遇到了一些困难。我下面的代码是用 JavaScript 编写的,我只是想澄清一下我在这里可能做错的事情。
<?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>
所以当我使用alert(data.earthquakes);时我得到了未定义的操作,这很好,但我知道我在这里得到了响应。然而,JSON 的架构如下:
{"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}
]}
所以我尝试了不同的方式来提取信息,例如 alert(data.earthquakes[1].eqid);
和 alert(data.earthquakes. eqid[1]);
,但是我没有得到想要的专用数组。
有人可以指导我
- 如何正确地获得所需的架构结果吗?
- 如果我想使用数组和 for 循环将所有元素提取到本地数组中,
该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的 JSON 对象定义如下:
试试这个:
这是一个很好的参考点: http://www .json.org/js.html
If your JSON object is defined as follows:
Try this:
This is a good point of reference: http://www.json.org/js.html