json数组存储了两个对象,要怎么遍历

{"all":[{"menuid":10,"pmenuid":0,"url":"javascript:void(0);","menuname":"系统管理","isnode":0,"sort":1,"level":1,"enable":0},{"menuid":1010,"pmenuid":10,"url":"/view/yhgl/yhcx.jsp","menuname":"用户管理","isnode":1,"sort":1,"level":2,"enable":0},{"menuid":1011,"pmenuid":10,"url":"/view/yhgl/yhxz.jsp","menuname":"角色管理","isnode":1,"sort":2,"level":2,"enable":0},{"menuid":11,"pmenuid":0,"url":"javascript:void(0);","menuname":"文章管理","isnode":0,"sort":2,"level":1,"enable":0},{"menuid":1110,"pmenuid":11,"url":"bus_article.html","menuname":"文章管理","isnode":1,"sort":1,"level":2,"enable":0},{"menuid":1111,"pmenuid":11,"url":"bus_article_check.html","menuname":"文章审核","isnode":1,"sort":2,"level":2,"enable":0},{"menuid":1112,"pmenuid":11,"url":"bus_article_seq.html","menuname":"文章排序","isnode":1,"sort":3,"level":2,"enable":0},{"menuid":12,"pmenuid":0,"url":"javascript:void(0);","menuname":"标签管理","isnode":0,"sort":3,"level":1,"enable":0},{"menuid":1210,"pmenuid":12,"url":"/view/bqgl/bqgl.jsp","menuname":"标签管理","isnode":1,"sort":1,"level":2,"enable":0},{"menuid":13,"pmenuid":0,"url":"javascript:void(0);","menuname":"工作统计","isnode":0,"sort":4,"level":1,"enable":0},{"menuid":1310,"pmenuid":13,"url":"bus_count.html","menuname":"工作统计","isnode":1,"sort":1,"level":2,"enable":0}],
"user":[{"menuid":10,"pmenuid":0,"menuname":"系统管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1010,"pmenuid":0,"menuname":"用户管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1011,"pmenuid":0,"menuname":"角色管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":11,"pmenuid":0,"menuname":"文章管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1110,"pmenuid":0,"menuname":"文章管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1111,"pmenuid":0,"menuname":"文章审核","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1112,"pmenuid":0,"menuname":"文章排序","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":12,"pmenuid":0,"menuname":"标签管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1210,"pmenuid":0,"menuname":"标签管理","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":13,"pmenuid":0,"menuname":"工作统计","isnode":0,"sort":0,"level":0,"enable":0},{"menuid":1310,"pmenuid":0,"menuname":"工作统计","isnode":0,"sort":0,"level":0,"enable":0}]}

在jquery里面,分别要取到“all”的所有menuid和menuname,也要取到“user”的所有menuid和menuname

for(var i in json.all)
    console.log(json.all[i].menuid + ": " + json.all[i].menuname);
for(var i in json.user)
    console.log(json.user[i].menuid + ": " + json.user[i].menuname);

或者

for(var key in json) {
    for(var i in json[key])
        console.log(json[key][i].menuid + ": " + json[key][i].menuname;
}

里面的json就是你的json对象。如果是文本格式,就先用JSON.parse转化一下

温馨提示:内容为网友见解,仅供参考
第1个回答  2017-08-16
for(var key in json){//key为键值 ,json[key]为value值
}
相似回答