在Javascript中遍历JSON对象?

时间:2013-08-27 09:38:10

标签: javascript jquery

下面是我的代码。请帮我循环一下。我想循环完整的JSON并做一些验证,但我无法循环它。我是第一次这样做,如果任何人能帮助我,那将是很好的。

有没有办法过滤JSON对象。例如,我想搜索auditor1 asgn 值。过滤器可以是动态的,就像它可以是auditor1或auditor11。我也想知道如何将上面的json转换为数组。这将使我的搜索变得简单(如果无法通过直接JSON搜索进行搜索)。

function fnMultiRowValidation(){
      var vStatus = 5,
          vJson = '{"tpaCo":[{"name":"Audit Company1",\
                             "aud":[{"name":"auditor1","asgn":"1","fnds":"1","lead":"1"},\
                                    {"name":"auditor2","asgn":"1","fnds":"0","lead":"1"},\
                                    {"name":"auditor3","asgn":"0","fnds":"1","lead":"0"},\
                                    {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor5","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor6","asgn":"0","fnds":"1","lead":"0"},\
                                    {"name":"auditor7","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor8","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor9","asgn":"0","fnds":"1","lead":"0"},\
                                    {"name":"auditor10","asgn":"1","fnds":"1","lead":"0"},\
                                    {"name":"auditor11","asgn":"1","fnds":"1","lead":"0"}]},\
                            {"name":"Audit Company2",\
                             "aud":[{"name":"auditor3","asgn":"1","fnds":"1","lead":"1"},\
                                    {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"}\
                                   ]\
                            }\
                          ]}';
          var vObj =  JSON.parse(vJson);


      for (var i=0;i<vObj.tpaCo.length;i++){
        $.each(vObj.tpaCo[i], function(key, value) { 
              console.log(key +':'+ value);
              if(typeof(value)=='object'){
                 //console.log('Auditor length:'+vObj.tpaCo.value.length);
              }
        });  
      }
    }

5 个答案:

答案 0 :(得分:1)

vObj.tpaCo.value.length

不起作用。您必须使用vObj.tpaCo[key].lengthvalue.length。对于初学者,不应将原生for循环与each迭代混合使用。

使用for - 和for-in - 循环:

for (var i=0; i<vObj.tpaCo.length; i++) { // iterate through outer array
    for (var key in vObj.tpaCo[i]) { // enumerate item keys
         console.log(key +':'+ vObj.tpaCo[i][key]); // logs "name" and "aud"
    }
    console.log('Auditor length:'+vObj.tpaCo[i].aud.length);
    for (var j=0; j<vObj.tpaCo[i].aud.length; j++) { // iterate "aud" array
        console.log(vObj.tpaCo[i].aud[j].name);
    }
}

使用变量简化:

var tpacos = vObj.tpaCo;
for (var i=0; i<tpacos.length; i++) {
    var comp = tpacos[i];
    for (var key in comp) {
         var value = comp[key];
         console.log(key +':'+ value);
    }
    var auds = comp.aud;
    console.log('Auditor length:'+auds.length);
    for (var j=0; j<auds.length; j++) {
        var aud = auds[j];
        console.log(aud.name);
    }
}

现在使用Array forEach method

vObj.tpaCo.forEach(function(comp, i) {
    for (var key in comp) {
         var value = comp[key];
         console.log(key +':'+ value);
    }
    console.log('Auditor length:'+comp.aud.length);
    comp.aud.forEach(function(aud, j) {
        console.log(aud.name);
    });
});

使用jQuery's each

$.each(vObj.tpaCo, function(i, comp) {
    $.each(comp, function(key, value) {
        console.log(key +':'+ value);
    });
    console.log('Auditor length:'+comp.aud.length);
    $.each(comp.aud, function(j, aud) {
        console.log(aud.name);
    });
});

答案 1 :(得分:0)

你的代码应该是这样的

$.each(vObj.tpaCo, function(key, value) { 
          console.log(key +':'+ value);
          if(typeof(value)=='object'){
             //console.log('Auditor length:'+vObj.tpaCo.value.length);
          }
});
希望它有所帮助......

答案 2 :(得分:0)

  function fnMultiRowValidation(){
  var vStatus = 5,
      vJson = '{"tpaCo":[{"name":"Audit Company1",\
                         "aud":[{"name":"auditor1","asgn":"1","fnds":"1","lead":"1"},\
                                {"name":"auditor2","asgn":"1","fnds":"0","lead":"1"},\
                                {"name":"auditor3","asgn":"0","fnds":"1","lead":"0"},\
                                {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor5","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor6","asgn":"0","fnds":"1","lead":"0"},\
                                {"name":"auditor7","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor8","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor9","asgn":"0","fnds":"1","lead":"0"},\
                                {"name":"auditor10","asgn":"1","fnds":"1","lead":"0"},\
                                {"name":"auditor11","asgn":"1","fnds":"1","lead":"0"}]},\
                        {"name":"Audit Company2",\
                         "aud":[{"name":"auditor3","asgn":"1","fnds":"1","lead":"1"},\
                                {"name":"auditor4","asgn":"1","fnds":"1","lead":"0"}\
                               ]\
                        }\
                      ]}';
      var vObj =  JSON.parse(vJson);


    $.each(vObj.tpaCo, function(key, value) { 
          console.log(value.name);
    });  
}

删除for循环。使用此脚本

要获取aud元素,你需要在当前循环中有一个$ .each函数

答案 3 :(得分:0)

我很确定你可以摆脱for循环,只需使用

$.each( vObk.tpaCo, function(key, value) {
  console.log(key + ':' + value);
  //... do stuff with value ...
})

答案 4 :(得分:0)

$.each(vObj.tpaCo, function(key, value) { 
              console.log(value.name+" ")
              for(i=0; i<value.aud.length; i++)
              console.log(value.aud[i]);
        }); 

或者如果你不知道名字

       $.each(vObj, function(key, value){
        $.each(value, function(key, value){
            for(val in value)
                  if(typeof val == 'string')
                    console.log(value[val]
                  else 
                  for(i=0; i<value[val].length ; i++)
                    console.log(value[val][i]);

        });
    });

http://jsfiddle.net/GxER7/