Loop over json object array with jquery

Im trying to iterate over an array of json file objects to access the keys and values ​​of its variables and add them to the list of elements using jquery getjson and each.

I think the solution should be similar to the solution in this article ... but I can not get it to work and show results in general ... Any help would be greatly appreciated!

JQuery sorting through a json array

$.getJSON('data/file.json', function(data)
 { 
   var data = [];
   $(data).each(function(idx, obj)
    { 
     $(obj).each(function(key, value)
      {console.log(key + ": " + value);}
     }
   }
);

Json data is formatted as follows:

[{
    "name": "Name",
    "street_address1": "XYZ Road",
    "street_address2": null,
    "city": "New York",
    "zip": 10038,
    "phone": 2122222222 ", 
    "description ": "About xyz..."
 }, 
 { next listing... }]

And html should be formatted as follows:

 Name: Name

 Address: XYZ Road
          Floor #2
          City, State 10001

 Description: About xyz...
+3
source share
3 answers

var data = [];

You replace with dataan empty array, thereby destroying your data when this is done. Delete this line and it should work.

EDIT. . each. :

$.getJSON('data/file.json', function(data){ 
    $(data).each(function(idx, obj){ 
        $(obj).each(function(key, value){
            console.log(key + ": " + value);
        });
    });
});

EDIT 2: data obj jQuery, . $.each $().each.

$.getJSON('data/file.json', function(data){ 
    $.each(data, function(idx, obj){ 
        $.each(obj, function(key, value){
            console.log(key + ": " + value);
        });
    });
});
+12
$.getJSON('data/file.json', function(data){ 
    $.each(data,function(idx, obj){ 
        $.each(obj, function(key, value){
            console.log(key + ": " + value);
        });
    });
});
0

The code has several syntax errors. Take a look at http://www.jslint.com/ , for example.

-1
source

All Articles