JSON Jquery 数组类型

发布于 2024-10-17 17:05:25 字数 1977 浏览 2 评论 0原文

我有一个关于 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]);,但是我没有得到想要的专用数组。

有人可以指导我

  1. 如何正确地获得所需的架构结果吗?
  2. 如果我想使用数组和 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

  1. how to get the desired architecture result appropriately and
  2. if I want to use array and for loop to extract all the elements into local array,

how to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮茶煮酒煮时光 2024-10-24 17:05:25

如果您的 JSON 对象定义如下:

var jsonData = {"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}
    ]};

试试这个:

var myData = eval('(' + jsonData + ')');
var firstElement = myData.earthquakes[0];

这是一个很好的参考点: http://www .json.org/js.html

If your JSON object is defined as follows:

var jsonData = {"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}
    ]};

Try this:

var myData = eval('(' + jsonData + ')');
var firstElement = myData.earthquakes[0];

This is a good point of reference: http://www.json.org/js.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文