looping over a json object array with jquery

前端 未结 3 912
野的像风
野的像风 2020-12-18 12:30

Im trying to loop through a json files\' object array to access its variables\' keys and values and append them to list items using jquery\'s getjson and each.

I th

相关标签:
3条回答
  • 2020-12-18 13:10

    var data = [];

    You are replacing data with a blank array, thus destroying your data when this is ran. Remove this line, and it should work.

    EDIT: There are also some syntax errors in your code. You need to close the parenthesis after each of the each statements. It should look like this:

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

    EDIT 2: data and obj aren't jQuery objects, they are just normal objects. You need to use $.each compared to $().each for this.

    $.getJSON('data/file.json', function(data){ 
        $.each(data, function(idx, obj){ 
            $.each(obj, function(key, value){
                console.log(key + ": " + value);
            });
        });
    });
    
    0 讨论(0)
  • 2020-12-18 13:11

    The code has multiple syntax errors. Check it on http://www.jslint.com/ for example.

    0 讨论(0)
  • 2020-12-18 13:13
    $.getJSON('data/file.json', function(data){ 
        $.each(data,function(idx, obj){ 
            $.each(obj, function(key, value){
                console.log(key + ": " + value);
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题