2013-06-27 163 views
0

还没看做了很多使用JSON在一段时间。循环通过JSON对象的jQuery

我有以下的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打印出每个类别及其所有评论?

回答

2

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

你必须做的,当然一些嵌套,沿

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

js解决东西线与DEMO应该与任何深度的任何非递归对象/ 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 
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

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); 
    } 
}