我有嵌套的json
,如下所示。我需要迭代json
并且必须得到每个值。
{
"processDefId": "xyz",
"name": "name",
"sla": "10",
"hasChild": true,
"child": [
{
"activityDefId": "siteSurveyDef",
"activityName": "Site Survey",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true,
"child": [
{
"activityDefId": "Begin",
"activityName": "Begin",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
},
{
"activityDefId": "Site Survey2",
"activityName": "Site Survey2",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
}
]
},
{
"activityDefId": "siteSurvey",
"activityName": "Procurement",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true,
"child": [
{
"activityDefId": "Begin",
"activityName": "Begin"
},
{
"activityDefId": "Site Survey",
"activityName": "Site Survey3",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
}
]
},
{
"activityDefId": "siteSurvey",
"activityName": "Provisioning",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true,
"child": [
{
"activityDefId": "Begin",
"activityName": "Begin"
},
{
"activityDefId": "Site Survey",
"activityName": "Site Survey4",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
}
]
}
]
}
我试着像这样循环:
$.each(hiddenJson, function() {
$.each(this, function(name, value) {
//var sla = me.sla;//use `this` from outer scope
//alert(sla);
console.log(name + '=' + value);
});
});
我没有得到正确的价值。任何人都可以建议吗?
答案 0 :(得分:1)
好的Recursively
你可以做到,
var j = {
"processDefId": "xyz",
"name": "name",
"sla": "10",
"hasChild": true,
"child": [
{
"activityDefId": "siteSurveyDef",
"activityName": "Site Survey",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true,
"child": [
{
"activityDefId": "Begin",
"activityName": "Begin",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
},
{
"activityDefId": "Site Survey2",
"activityName": "Site Survey2",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
}
]
},
{
"activityDefId": "siteSurvey",
"activityName": "Procurement",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true,
"child": [
{
"activityDefId": "Begin",
"activityName": "Begin"
},
{
"activityDefId": "Site Survey",
"activityName": "Site Survey3",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
}
]
},
{
"activityDefId": "siteSurvey",
"activityName": "Provisioning",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true,
"child": [
{
"activityDefId": "Begin",
"activityName": "Begin"
},
{
"activityDefId": "Site Survey",
"activityName": "Site Survey4",
"isMandatory": "true",
"isOptional": "false",
"sla": "10",
"sequence": "1",
"hasChild": true
}
]
}
]
};
function recursiveDecode(obj){
$.each(obj, function(n, v) {
if(typeof v === 'object'){
recursiveDecode(v);
}
else{
console.log(n +'='+v);
}
});
};
recursiveDecode(j);