通过AJAX阅读嵌套的JSON与jQuery或JavaScript和输出到表

问题描述:

可能重复:
   jQuery的阅读嵌套的JSON

我真的想通过JSON多个记录有一个硬性的方法来循环中,每个具有潜在的深层嵌套。我只是想输出到一个表。

I would really like to have a hard and fast method to loop through multiple records in JSON, each with potentially deep nesting. I simply want to output to a table.

我不能确定的参数需要通过()的函数的调用$每个()或为$。阿贾克斯()成功的JavaScript方法通过什么。所有的例子似乎使用通用词数据或目标文件,但这些让我困惑 - 他们是内置的功能参数或我可以说出他们的任何我想这样的:

I am unsure of what arguments need to be passed through the function() for either $.each() or for the javascript methodology of $.ajax() success. All examples seem to use generic words "data" or "obj" but those confuse me - are they built-in functional arguments or can I name them whatever I want like:

$.each(foo, function(bar){

});

和我怎么可以让我在哪里,在环/窝的轨道上吗?

And how can I keep track of where I am in the loop / nest?

我会preFER使用jQuery,但我也应该知道直接的JavaScript方法来做到这一点。而且我也想知道是否有可能没有几百行code。

I would prefer to use JQuery, but I should also know the straight-forward JavaScript method to do so. And I'd also like to know if it's possible without a hundred lines of code.

使用此JSON作为一个例子:

With this JSON as an example:

{
date: " 2012-10-18 16:58:35.104576",
data: [
    {
        title: "The Princess Bride",
        rating: "PG", 
        length: 128,
        stars : [
                "Gary Elwes",
                "Robin Wright",
                "Christopher Guest"
            ]

    },
    {
        title: "This is Spinal Tap",
        rating: "R",
        length: 105,
        stars: [
                "Christopher Guest",
                "Michael McKean",
                "Harry Shearer"
            ]
    }
]
}

我找不到包含嵌套的JSON任何有用的例子,即使在这里的计算器。

I can't find any useful examples that include nested JSON, even here in the *.

什么通过最有效的方式来循环和每个元素分配给表格单元?该HTML输出并不重要 - 我知道如何使一个表...我如何获得明星

what's the most efficient way to loop through and assign each element to a table cell? The HTML output is not important - I know how to make a table... How do I get the "stars"?

当我使用Chrome控制台和简单的 $的getJSON('/例如'); 我得到了整个JSON返回的responseText的,从{日期:2012年10月18号,数据:[{称号:公主新娘,然而,在既没有JSON文档,JQuery的文档上$ .getJSON,也没有在任何JavaScript的例子可以找到一个参照的responseText 。所以,我迷路了。什么争论并$ .getJSON需要客体的responseText的?

When I use the Chrome console and simply $.getJSON('/example'); I get the entire JSON returned in the responseText, starting with "{"date":"2012-10-18 ,"data": [{"title": "The Princess Bride", However in neither the JSON docs, the JQuery docs on $.getJSON, nor in any JavaScript examples can I find a reference to responseText. So, I'm lost. What argument does $.getJSON need to objectify the responseText?

尝试

obj.data [0] .stars //会给你第一个对象的星星

obj.data[0].stars // Will give you the stars in 1st Object

obj.data [0] .stars [0] //加里Elwes

obj.data[0].stars[0] // Gary Elwes

FIDDLE

要迭代直通明星对象,你可以试试这个

To iterate thru the Stars object You can try this

$.each(obj.data , function(key , value){ // First Level
    $.each(value.stars , function(k , v ){  // The contents inside stars
        alert(v)
    });        

});

修订FIDDLE

修改

 $.ajax({
      // Parameters 

      success : function(obj){
            $.each(obj.data , function(key , value){ // First Level
                 $.each(value.stars , function(k , v ){  // The contents inside stars
                     alert(v)
                 });     
            });
      }
  });