[英]Traverse JSON object in Javascript?
below is my code. 下面是我的代码。 please help me loop through it.
请帮助我循环浏览。 i want to loop the complete JSON and do some validation, but i am not able to loop through it.
我想循环播放完整的JSON并进行一些验证,但是我无法循环通过它。 i am doing it for the first time , it would be nice if any1 can help me.
我是第一次这样做,如果any1可以帮助我,那就太好了。
Is there any way to filter the JSON object. 有什么办法可以过滤JSON对象。 for example i want to search auditor1 asgn value.
例如,我要搜索audit1的asgn值。 filter can be dynamic like it can be auditor1 or auditor11.
filter可以是动态的,就像可以是audit1或audit11。 also i want to knw how can i convert the above json into array.
我也想知道如何将上述json转换为数组。 which will make my search easy(in case there is no way to search by direct JSON search).
这将使我的搜索变得容易(如果无法通过直接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);
}
});
}
}
vObj.tpaCo.value.length
won't work. 将无法正常工作。 You either had to use
vObj.tpaCo[key].length
or value.length
. 您必须使用
vObj.tpaCo[key].length
或value.length
。 For a beginner, you shouldn't mix native for-loops with each
iteration. 对于初学者,您不应在
each
迭代中都混入本机for循环。
Using for - and for-in -loops: 使用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);
}
}
Simplified by using variables: 通过使用变量简化:
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);
}
}
Now with the Array forEach
method : 现在使用Array
forEach
方法 :
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);
});
});
And with jQuery's each
: 和jQuery的
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);
});
});
your code should be like this 您的代码应该像这样
$.each(vObj.tpaCo, function(key, value) {
console.log(key +':'+ value);
if(typeof(value)=='object'){
//console.log('Auditor length:'+vObj.tpaCo.value.length);
}
});
hope it helps..... 希望能帮助到你.....
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);
});
}
remove for loop. 删除循环。 Use this script
使用此脚本
To get aud elements, u need to have a $.each function inside the current loop 要获取aud元素,您需要在当前循环中具有$ .each函数
I'm pretty sure you can get rid of the for
loop, and just use 我很确定您可以摆脱
for
循环,而只需使用
$.each( vObk.tpaCo, function(key, value) {
console.log(key + ':' + value);
//... do stuff with value ...
})
$.each(vObj.tpaCo, function(key, value) {
console.log(value.name+" ")
for(i=0; i<value.aud.length; i++)
console.log(value.aud[i]);
});
or if you don't know the names 或者如果您不知道名字
$.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]);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.