json数据格式:一个多层json,对应的遍历取内部的值。
{ "code": 0, "message": "成功", "data": [ { "id": "9", "name": "工具", "O2OSmallCategroy": [ { "id": "2030", "name": "锤/锉/锯/刀/尺", "SmallIndustry": [ { "id": "44182", "name": "综合套装" }, { "id": "44184", "name": "电讯工具套装" }] } ] } ] }
代码是通过ajax返回json数据后的内部jquery拼接。
var strData = ‘‘; var data = json.data; $.each(data, function (index, item) { strData += ‘<li class=\"ztree_one\"><div><span class=\"root_colse root_open\"></span><a href=\"javascript:;\">‘ + item["name"] + ‘</a></div>‘; if (item["O2OSmallCategroy"].length != 0) { strData += ‘<ul>‘; $.each(item["O2OSmallCategroy"], function (i, i_item) { strData += ‘<li class=\"ztree_two\"><div><span class=\"root_colse root_open\"></span><a href=\"javascript:;\">‘ + i_item["name"] + ‘<i class=\"tree_del\" data-txt=\"2_‘ + i_item["id"] + ‘\"></i></a></div>‘; $.each(i_item["SmallIndustry"], function (j, j_item) { strData += ‘<ul><li><span class=\"tree_docu\"></span><a href=\"javascript:;\">‘ + j_item["name"] + ‘<i class=\"tree_del\" data-txt=\"3_‘ + j_item["id"] + ‘\"></i></a></li></ul>‘; }); strData += ‘</li>‘; }); strData += ‘</ul>‘; } });