ajax中如何把传递过来的json数据循环遍历出来显示

qq_37115057 2018-01-10 10:30:21
{
"list":[
{
"name":"用户管理",
"menuId":1,
"pageUrl":"/user",
"sort":2,
"type":"0",
"list":[
{
"name":"工人管理",
"menuId":10,
"pageUrl":"/user/index.jsp?roleKey=worker",
"sort":1,
"type":"1",
"parentId":1
},
{
"name":"工长管理",
"menuId":11,
"pageUrl":"/user/index.jsp?roleKey=captain",
"sort":2,
"type":"1",
"parentId":1
}
],
"parentId":0
},
{
"name":"工单管理",
"menuId":2,
"pageUrl":"/case",
"sort":2,
"type":"0",
"list":[
{
"name":"预约",
"menuId":12,
"pageUrl":"/case/index.jsp?cateId=13",
"sort":1,
"type":"1",
"parentId":2
},
{
"name":"咨询",
"menuId":13,
"pageUrl":"/case/index.jsp?cateId=14",
"sort":2,
"type":"1",
"parentId":2
}

],
"parentId":0
}
]
}

json返回这种格式怎么把他显示出来
...全文
1821 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
感谢一楼大佬
戎码亿升 2018-01-22
  • 打赏
  • 举报
回复
一楼刚好用一个递归方法解决很是巧妙
天际的海浪 2018-01-10
  • 打赏
  • 举报
回复

<div id="box"></div>

<script type="text/javascript">
var data = {
"list":[
        {
            "name":"用户管理",
            "menuId":1,
            "pageUrl":"/user",
            "sort":2,
            "type":"0",
            "list":[
                {
                    "name":"工人管理",
                    "menuId":10,
                    "pageUrl":"/user/index.jsp?roleKey=worker",
                    "sort":1,
                    "type":"1",
                    "parentId":1
                },
                {
                    "name":"工长管理",
                    "menuId":11,
                    "pageUrl":"/user/index.jsp?roleKey=captain",
                    "sort":2,
                    "type":"1",
                    "parentId":1
                }
            ],
            "parentId":0
        },
        {
            "name":"工单管理",
            "menuId":2,
            "pageUrl":"/case",
            "sort":2,
            "type":"0",
            "list":[
                {
                    "name":"预约",
                    "menuId":12,
                    "pageUrl":"/case/index.jsp?cateId=13",
                    "sort":1,
                    "type":"1",
                    "parentId":2
                },
                {
                    "name":"咨询",
                    "menuId":13,
                    "pageUrl":"/case/index.jsp?cateId=14",
                    "sort":2,
                    "type":"1",
                    "parentId":2
                }
               
            ],
            "parentId":0
        }
   ]
};


function fu(arr) {
	var str = "";
	for (var i = 0, len = arr.length; i < len; i++) {
		str += "<li>"+arr[i].name+"</li>";
		if (arr[i].list)
			str += fu(arr[i].list);
	}
	return "<ul>"+str+"</ul>";
}
document.getElementById("box").innerHTML=fu(data.list);

</script>

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧