使用jquery循环遍历json对象

时间:2013-06-27 14:39:23

标签: jquery json

在一段时间内没有对json做过多少工作。

我有以下json对象

[{"Id":1,"Category":"Category1","Comments":[
    {"Id":1,"Comment":"test"},
    {"Id":2,"Comment":"test 2"}
]},
 {"Id":2,"Category":"Category2","Comments":[
    {"Id":1,"Comment":"test"}
]},
 {"Id":3,"Category":"Category3","Comments":[
    {"Id":1,"Comment":"something"},
    {"Id":2,"Comment":"test 2"},
    {"Id":3,"Comment":"test 3"},
    {"Id":4,"Comment":"something 4"},
    {"Id":5,"Comment":"something 5"}
]}]

使用jQuery / JS来打印每个类别及其所有注释的最佳方法是什么?

4 个答案:

答案 0 :(得分:2)

使用$.each(json, function(index, jsonObject)

当然,你必须做一些嵌套,这些都是

$.each(json, function(index, jsonObject){
    console.log(jsonObject.Category);
    $.each(jsonObject.Comments, function(i, comment){
        console.log(comment);
    });
});

答案 1 :(得分:2)

带有DEMO的纯js解决方案应该适用于任何深度的任何非递归对象/ JSON对象:

var jsonObj = [{"Id":1,"Category":"Category1","Comments":[{"Id":1,"Comment":"test"},{"Id":2,"Comment":"test 2"}]},{"Id":2,"Category":"Category2","Comments":[{"Id":1,"Comment":"test"}]},{"Id":3,"Category":"Category3","Comments":[{"Id":1,"Comment":"something"},            {"Id":2,"Comment":"test 2"},            {"Id":3,"Comment":"test 3"},            {"Id":4,"Comment":"something 4"},            {"Id":5,"Comment":"something 5"}        ]}];

var obj2String = function (obj, indent) {   // recursive method to print out all key-value pairs within an object
    indent = indent ? indent+'  ':' ';       // every level should be indented 2 spaces more
    if (typeof obj !== 'object') {          // if it's a simple value (not obj or array)
        return [indent, obj, '\n'].join('');// add it to the string (with a new line at the end)
    } else { // otherwise 
        var ret = '';
        for (var key in obj) {              // loop through the object
            if (obj.hasOwnProperty(key)) {  // check if the key refers to one of its values (and not the prototype)
                // if yes add the "key: " + the result objs2String(value) with correct indentation
                ret += [indent, key+':\n', obj2String(obj[key], indent)].join(''); 
            }
        }
        return (obj.indexOf ?               // return the results wrapped in a [] or {} depending on if it's an object or array (by checking the indexOf method. of course this delivers false results if the object has an indexOf method)
            [indent, '[\n', ret, indent, ']']:[indent, '{\n', ret, indent, '}']).join('');
    }
}

console.log(obj2String(jsonObj));           // print out the result

答案 2 :(得分:1)

此功能将打印出对象的每个成员,可以自定义以仅打印感兴趣的对象。编辑:更新小提琴updated fiddle

var v= [{"Id":1,"Category":"Category1","Comments":[
        {"Id":1,"Comment":"test"},
        {"Id":2,"Comment":"test 2"}
    ]},
     {"Id":2,"Category":"Category2","Comments":[
        {"Id":1,"Comment":"test"}
    ]},
     {"Id":3,"Category":"Category3","Comments":[
        {"Id":1,"Comment":"something"},
        {"Id":2,"Comment":"test 2"},
        {"Id":3,"Comment":"test 3"},
        {"Id":4,"Comment":"something 4"},
        {"Id":5,"Comment":"something 5"}
    ]}];
var rf=function recf(index,value){
console.log(value);
    if (value instanceof Object){
        $.each(value,function(val,idx){recf(val,idx)});
    }
};


$.each(v,rf);

Here's my fiddle

答案 3 :(得分:1)

如果你使用纯Javascript,那么:

data是json数据

for (var i in data)
{
    console.log("category:"+data[i].Category);
    for(var j in data[i].Comments)
    {
        console.log(data[i].Comments[j].Comment);
    }
}