使用jquery循环json对象

使用jquery循环json对象,jquery,json,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":"Category

已经有一段时间没有对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循环打印每个类别及其所有注释的最佳方法是什么

使用
$。每个(json、函数(索引、jsonObject)

当然,你必须做一些筑巢,沿着

$.each(json, function(index, jsonObject){
    console.log(jsonObject.Category);
    $.each(jsonObject.Comments, function(i, comment){
        console.log(comment);
    });
});
使用
$。每个(json,函数(索引,jsonObject)

当然,你必须做一些筑巢,沿着

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

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

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

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


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


如果使用纯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);
    }
}

如果使用纯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);
    }
}