Reading nested JSON via AJAX with jquery or javascript and outputting to tables

Possible duplicate:
jquery reading nested json

I would really like to have a complex and fast method for looping through multiple entries in JSON, each of which has a potentially deep nesting. I just want to put it on the table.

I'm not sure what arguments need to be passed through the function () for $ .each () or for the javascript success methodology $ .ajax (). All the examples seem to use the common words "data" or "obj", but this confused me - these are built-in functional arguments, or I can call them what I want:

$.each(foo, function(bar){ }); 

And how can I track where I am in the loop / socket?

I would rather use jQuery, but I should also know the direct JavaScript method for this. And I would also like to know if this is possible without hundreds of lines of code.

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" ] }] } 

I cannot find useful examples that include nested JSON, even here in StackOverflow.

What is the most efficient way to scroll and snap each item to a table cell? HTML output is not important - I know how to make a table ... How to get "stars"?

When I use the Chrome console and just $.getJSON('/example'); , I get all the JSON returned in responseText, starting with "{"date":"2012-10-18 ,"data": [{"title": "The Princess Bride", However, in both JSON documents, there are $ JQuery documents .getJSON and in no JavaScript examples can I find a link to responseText . So, I'm lost. What argument does $ .getJSON need to objectify responseText?

+7
source share
1 answer

try

 obj.data[0].stars // Will get you the stars in the 1st Object obj.data[0].stars[0] // Gary Elwes 

Fiddle

Iterate through Stars object. You can try this.

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

UPDATED FIDDLE

EDIT

  $.ajax({ // Parameters success : function(obj){ $.each(obj.data , function(key , value){ // First Level $.each(value.stars , function(k , v ){ // The contents inside stars console.log(v) }); }); } }); 
+8
source

All Articles