js 解析JSON

// 初始化菜单栏
function initMenu(menu) {
    var result = eval("(" + menu + ")");
    var data = result.data[0].children;
    if (data.length == 0) {
        self.location = "/logout.jsp";
        return;
    }
    // 加载菜单栏
    for ( var i = 0; i < data.length; i++) {
        var content = "<div style='margin: 10px;'>";
        for (var j = 0; j < data[i].children.length; j++) {
            if (data[i].children[j].value.indexOf("-") == 0)
                continue;
            content += "<div style='margin: 5px;'><a href='#' onclick="javascript: addTab('"
                    + data[i].children[j].name
                    + "', '"
                    + data[i].children[j].value.replace("(.*)", "")
                    + "');">"
                    + data[i].children[j].name + "</a></div>";
        }
        content += "</div>";
        $("#manage-console").accordion("add", {
            id : data[i].value,
            title : data[i].name,
            content : content,
            selected : false
        });
    }
}
<scripttype="text/javascript">//初始化$(function(){
    initMenu('{
        "data": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "id": "MN03004",
                                "leaf": true,
                                "name": "部门管理",
                                "status": 0,
                                "value": "/admin/hr/company(.*).action?companyID=0c522986-67ed-4ae2-8fcc-31dbe5e1ccf8",
                                "version": 0
                            },
                            {
                                "id": "MN03003",
                                "leaf": true,
                                "name": "子部门管理",
                                "status": 0,
                                "value": "/admin/hr/position(.*).action?companyID=0c522986-67ed-4ae2-8fcc-31dbe5e1ccf8",
                                "version": 0
                            },
                            {
                                "id": "MN03005",
                                "leaf": true,
                                "name": "人员管理",
                                "status": 0,
                                "value": "/admin/hr/employee(.*).action?companyID=0c522986-67ed-4ae2-8fcc-31dbe5e1ccf8",
                                "version": 0
                            },
                            {
                                "id": "MN03020",
                                "leaf": true,
                                "name": "报表管理",
                                "status": 0,
                                "value": "/admin/hr/report(.*).action?companyID=0c522986-67ed-4ae2-8fcc-31dbe5e1ccf8",
                                "version": 0
                            },
                            {
                                "id": "MN03023",
                                "leaf": true,
                                "name": "待办事项提醒",
                                "status": 0,
                                "value": "/admin/hr/task(.*).action?companyID=0c522986-67ed-4ae2-8fcc-31dbe5e1ccf8",
                                "version": 0
                            }
                        ],
                        "id": "MG03004",
                        "leaf": false,
                        "name": "公司A",
                        "status": 0,
                        "version": 0
                    },
                    {
                        "children": [
                            {
                                "id": "MN03009",
                                "leaf": true,
                                "name": "部门管理",
                                "status": 0,
                                "value": "/admin/hr/company(.*).action?companyID=4f079250-de19-4096-8f4d-ddc288a9e74e",
                                "version": 0
                            },
                            {
                                "id": "MN03010",
                                "leaf": true,
                                "name": "子部门管理",
                                "status": 0,
                                "value": "/admin/hr/position(.*).action?companyID=4f079250-de19-4096-8f4d-ddc288a9e74e",
                                "version": 0
                            },
                            {
                                "id": "MN03011",
                                "leaf": true,
                                "name": "人员管理",
                                "status": 0,
                                "value": "/admin/hr/employee(.*).action?companyID=4f079250-de19-4096-8f4d-ddc288a9e74e",
                                "version": 0
                            },
                            {
                                "id": "MN03018",
                                "leaf": true,
                                "name": "报表管理",
                                "status": 0,
                                "value": "/admin/hr/report(.*).action?companyID=4f079250-de19-4096-8f4d-ddc288a9e74e",
                                "version": 0
                            },
                            {
                                "id": "MN03019",
                                "leaf": true,
                                "name": "待办事项提醒",
                                "status": 0,
                                "value": "/admin/hr/task(.*).action?companyID=4f079250-de19-4096-8f4d-ddc288a9e74e",
                                "version": 0
                            }
                        ],
                        "id": "MG03006",
                        "leaf": false,
                        "name": "公司B",
                        "status": 0,
                        "version": 0
                    },
                    {
                        "children": [
                            {
                                "id": "MN03012",
                                "leaf": true,
                                "name": "部门管理",
                                "status": 0,
                                "value": "/admin/hr/company(.*).action?companyID=7dc063d8-94ef-43a8-8a31-8a8cc0384f3f",
                                "version": 0
                            },
                            {
                                "id": "MN03013",
                                "leaf": true,
                                "name": "子部门管理",
                                "status": 0,
                                "value": "/admin/hr/position(.*).action?companyID=7dc063d8-94ef-43a8-8a31-8a8cc0384f3f",
                                "version": 0
                            },
                            {
                                "id": "MN03014",
                                "leaf": true,
                                "name": "人员管理",
                                "status": 0,
                                "value": "/admin/hr/employee(.*).action?companyID=7dc063d8-94ef-43a8-8a31-8a8cc0384f3f",
                                "version": 0
                            },
                            {
                                "id": "MN03021",
                                "leaf": true,
                                "name": "报表管理",
                                "status": 0,
                                "value": "/admin/hr/report(.*).action?companyID=7dc063d8-94ef-43a8-8a31-8a8cc0384f3f",
                                "version": 0
                            },
                            {
                                "id": "MN03024",
                                "leaf": true,
                                "name": "待办事项提醒",
                                "status": 0,
                                "value": "/admin/hr/task(.*).action?companyID=7dc063d8-94ef-43a8-8a31-8a8cc0384f3f",
                                "version": 0
                            }
                        ],
                        "id": "MG03007",
                        "leaf": false,
                        "name": "公司C",
                        "status": 0,
                        "version": 0
                    },
                    {
                        "children": [
                            {
                                "id": "MN03015",
                                "leaf": true,
                                "name": "部门管理",
                                "status": 0,
                                "value": "/admin/hr/company(.*).action?companyID=b50c3ae9-47ef-4bd6-b38d-6c8654cf421d",
                                "version": 0
                            },
                            {
                                "id": "MN03016",
                                "leaf": true,
                                "name": "子部门管理",
                                "status": 0,
                                "value": "/admin/hr/position(.*).action?companyID=b50c3ae9-47ef-4bd6-b38d-6c8654cf421d",
                                "version": 0
                            },
                            {
                                "id": "MN03017",
                                "leaf": true,
                                "name": "人员管理",
                                "status": 0,
                                "value": "/admin/hr/employee(.*).action?companyID=b50c3ae9-47ef-4bd6-b38d-6c8654cf421d",
                                "version": 0
                            },
                            {
                                "id": "MN03022",
                                "leaf": true,
                                "name": "报表管理",
                                "status": 0,
                                "value": "/admin/hr/report(.*).action?companyID=b50c3ae9-47ef-4bd6-b38d-6c8654cf421d",
                                "version": 0
                            },
                            {
                                "id": "MN03025",
                                "leaf": true,
                                "name": "待办事项提醒",
                                "status": 0,
                                "value": "/admin/hr/task(.*).action?companyID=b50c3ae9-47ef-4bd6-b38d-6c8654cf421d",
                                "version": 0
                            }
                        ],
                        "id": "MG03008",
                        "leaf": false,
                        "name": "公司D",
                        "status": 0,
                        "version": 0
                    },
                    {
                        "children": [
                            {
                                "id": "MN03008",
                                "leaf": true,
                                "name": "同步数据",
                                "status": 0,
                                "value": "/admin/hr/companySyncEap.action",
                                "version": 0
                            }
                        ],
                        "id": "MG03005",
                        "leaf": false,
                        "name": "系统管理",
                        "status": 0,
                        "version": 0
                    }
                ],
                "id": "MG03",
                "leaf": false,
                "name": "人事系统",
                "status": 0,
                "version": 0
            }
        ],
        "success": true
    }');
});</script>
View JSON Code
// 打开Tab页
function addTab(title, uri) {
    if(uri.indexOf("_") == 0)
        window.open(uri.substring(1));
    else {
        $("#manage-tabs").tabs("close", title);
        uri += uri.indexOf("?") > -1 ? "&" : "?";
        uri += "rand=" + Math.random();
        $("#manage-tabs").tabs("add", {
            title : title,
            content : "<iframe scrolling='auto' src='" + uri + "' frameborder='0' style='100%; height:100%;'></iframe>",
            closable : true
        });
    }
}
View addTab()
原文地址:https://www.cnblogs.com/yushouling/p/4871560.html