jQuery遍历复杂的JSON数据
{"group_id_1":[{"groupd_id":"1","authority":{"19":"\u6d4f\u89c8\u6587\u7ae0","22":"\u5220\u9664\u6587\u7ae0","5":"\u6743\u9650\u7f16\u8f91","12":"\u6dfb\u52a0\u6743\u9650\u7ec4","14":"\u5220\u9664\u6743\u9650\u7ec4"}}],"group_id_2":[{"groupd_id":"2","authority":{"19":"\u6d4f\u89c8\u6587\u7ae0","20":"\u6dfb\u52a0\u6587\u7ae0","22":"\u5220\u9664\u6587\u7ae0","3":"\u6743\u9650\u6d4f\u89c8","4":"\u6dfb\u52a0\u6743\u9650","5":"\u6743\u9650\u7f16\u8f91","6":"\u6743\u9650\u5220\u9664","11":"\u6d4f\u89c8\u6743\u9650\u7ec4","12":"\u6dfb\u52a0\u6743\u9650\u7ec4","13":"\u7f16\u8f91\u6743\u9650\u7ec4","14":"\u5220\u9664\u6743\u9650\u7ec4","37":"\u7528\u6237\u65e5\u5fd7\u67e5\u770b","38":"\u63a8\u9001\u65e5\u5fd7\u67e5\u770b","39":"\u91c7\u96c6\u65e5\u5fd7\u67e5\u770b","40":"\u7528\u6237\u767b\u5f55\u65e5\u5fd7","41":"\u4e2a\u4eba\u4fe1\u606f\u4fee\u6539","31":"\u91c7\u96c6\u8ba1\u5212\u6d4f\u89c8","32":"\u6dfb\u52a0\u91c7\u96c6\u8ba1\u5212","33":"\u7f16\u8f91\u91c7\u96c6\u8ba1\u5212","34":"\u5220\u9664\u91c7\u96c6\u8ba1\u5212","35":"\u63a8\u9001\u65e5\u62a5\u8868\u67e5\u770b","36":"\u91c7\u96c6\u65e5\u62a5\u8868\u67e5\u770b"}}],"group_id_4":[{"groupd_id":"4","authority":{"3":"\u6743\u9650\u6d4f\u89c8","4":"\u6dfb\u52a0\u6743\u9650","6":"\u6743\u9650\u5220\u9664","11":"\u6d4f\u89c8\u6743\u9650\u7ec4"}}],"group_id_5":[{"groupd_id":"5","authority":{"20":"\u6dfb\u52a0\u6587\u7ae0","3":"\u6743\u9650\u6d4f\u89c8","6":"\u6743\u9650\u5220\u9664","13":"\u7f16\u8f91\u6743\u9650\u7ec4","37":"\u7528\u6237\u65e5\u5fd7\u67e5\u770b"}}],"group_id_6":[{"groupd_id":"6","authority":{"19":"\u6d4f\u89c8\u6587\u7ae0","20":"\u6dfb\u52a0\u6587\u7ae0","21":"\u7f16\u8f91\u6587\u7ae0","22":"\u5220\u9664\u6587\u7ae0","3":"\u6743\u9650\u6d4f\u89c8","4":"\u6dfb\u52a0\u6743\u9650","5":"\u6743\u9650\u7f16\u8f91","6":"\u6743\u9650\u5220\u9664","11":"\u6d4f\u89c8\u6743\u9650\u7ec4","12":"\u6dfb\u52a0\u6743\u9650\u7ec4","13":"\u7f16\u8f91\u6743\u9650\u7ec4","14":"\u5220\u9664\u6743\u9650\u7ec4","37":"\u7528\u6237\u65e5\u5fd7\u67e5\u770b","38":"\u63a8\u9001\u65e5\u5fd7\u67e5\u770b","39":"\u91c7\u96c6\u65e5\u5fd7\u67e5\u770b","40":"\u7528\u6237\u767b\u5f55\u65e5\u5fd7","41":"\u4e2a\u4eba\u4fe1\u606f\u4fee\u6539","31":"\u91c7\u96c6\u8ba1\u5212\u6d4f\u89c8","32":"\u6dfb\u52a0\u91c7\u96c6\u8ba1\u5212","33":"\u7f16\u8f91\u91c7\u96c6\u8ba1\u5212","34":"\u5220\u9664\u91c7\u96c6\u8ba1\u5212","35":"\u63a8\u9001\u65e5\u62a5\u8868\u67e5\u770b","36":"\u91c7\u96c6\u65e5\u62a5\u8868\u67e5\u770b"}}]}
一切都来源于上面这一组JSON数据,用JSON解析器可以看到这个JSON的结构,如下:
{ "group_id_1": [ { "groupd_id": "1", "authority": { "5": "权限编辑", "12": "添加权限组", "14": "删除权限组", "19": "浏览文章", "22": "删除文章" } } ], "group_id_2": [ { "groupd_id": "2", "authority": { "3": "权限浏览", "4": "添加权限", "5": "权限编辑", "6": "权限删除", "11": "浏览权限组", "12": "添加权限组", "13": "编辑权限组", "14": "删除权限组", "19": "浏览文章", "20": "添加文章", "22": "删除文章", "31": "采集计划浏览", "32": "添加采集计划", "33": "编辑采集计划", "34": "删除采集计划", "35": "推送日报表查看", "36": "采集日报表查看", "37": "用户日志查看", "38": "推送日志查看", "39": "采集日志查看", "40": "用户登录日志", "41": "个人信息修改" } } ], "group_id_4": [ { "groupd_id": "4", "authority": { "3": "权限浏览", "4": "添加权限", "6": "权限删除", "11": "浏览权限组" } } ], "group_id_5": [ { "groupd_id": "5", "authority": { "3": "权限浏览", "6": "权限删除", "13": "编辑权限组", "20": "添加文章", "37": "用户日志查看" } } ], "group_id_6": [ { "groupd_id": "6", "authority": { "3": "权限浏览", "4": "添加权限", "5": "权限编辑", "6": "权限删除", "11": "浏览权限组", "12": "添加权限组", "13": "编辑权限组", "14": "删除权限组", "19": "浏览文章", "20": "添加文章", "21": "编辑文章", "22": "删除文章", "31": "采集计划浏览", "32": "添加采集计划", "33": "编辑采集计划", "34": "删除采集计划", "35": "推送日报表查看", "36": "采集日报表查看", "37": "用户日志查看", "38": "推送日志查看", "39": "采集日志查看", "40": "用户登录日志", "41": "个人信息修改" } } ] }
(后台为了便于识别,写成了这样子,最初版本是精简版本的。)
于是用了$.each()来遍历之,写了好几层...
var obj = group_authority_list; $.each(obj, function(k, v){ if(k == "group_id_"+urlId){ $.each(v, function(kk, vv){ $.each(vv, function(kkk, vvv){ for(var i in vvv){ if(vvv[i] == urlId)continue; _inputAuthList.each(function() { if( $(this).val() == i ){ $(this).parent('span').addClass('checked'); } }); } }); }); } });
目标是把遍历到的对应权限,在输入框中选中,于是各位可以感受一下...
// 求轻拍砖