ajax做json拼接

来源:互联网 发布:fast paxos java示例 编辑:程序博客网 时间:2024/05/27 16:41
[
    {
        "id": "100000",
        "menu": [
            {
                "text": "部门管理",
                "items": [
                    {
                        "id": "100006",
                        "text": "部门增加",
                        "href": "http://localhost:8080/RBAC/Department/Department_add.jsp"
                    },
                    {
                        "id": "100007",
                        "text": "部门维护",
                        "href": "http://localhost:8080/RBAC/selectDepartment"
                    }
                ]
            }
        ]
    },
    {
        "id": "100001",
        "menu": [
            {
                "text": "用户管理",
                "items": [
                    {
                        "id": "100008",
                        "text": "用户增加",
                        "href": "http://localhost:8080/RBAC/getDeptNameList"
                    },
                    {
                        "id": "100009",
                        "text": "用户维护",
                        "href": "http://localhost:8080/RBAC/selectUser"
                    }
                ]
            }
        ]
    },
    {
        "id": "100002",
        "menu": [
            {
                "text": "角色管理",
                "items": [
                    {
                        "id": "100010",
                        "text": "角色增加",
                        "href": "http://localhost:8080/RBAC/Role/role_add.jsp"
                    },
                    {
                        "id": "100011",
                        "text": "角色维护",
                        "href": "http://localhost:8080/RBAC/selectRole"
                    }
                ]
            }
        ]
    },
    {
        "id": "100003",
        "menu": [
            {
                "text": "菜单管理",
                "items": [
                    {
                        "id": "100012",
                        "text": "菜单增加",
                        "href": "http://localhost:8080/RBAC/selectFatherMenu"
                    },
                    {
                        "id": "100013",
                        "text": "菜单维护",
                        "href": "http://localhost:8080/RBAC/selectMenu"
                    }
                ]
            }
        ]
    },
    {
        "id": "100004",
        "menu": [
            {
                "text": "角色人员管理",
                "items": [
                    {
                        "id": "100014",
                        "text": "角色人员维护",
                        "href": "http://localhost:8080/RBAC/selectRoleUser"
                    }
                ]
            }
        ]
    },
    {
        "id": "100005",
        "menu": [
            {
                "text": "权限管理",
                "items": [
                    {
                        "id": "100015",
                        "text": "用户权限设置",
                        "href": "http://localhost:8080/RBAC/selectUserMenu"
                    },
                    {
                        "id": "100016",
                        "text": "部门权限设置",
                        "href": "http://localhost:8080/RBAC/selectDeptMenu"
                    },
                    {
                        "id": "100017",
                        "text": "角色权限设置",
                        "href": "http://localhost:8080/RBAC/selectRoleMenu"
                    }
                ]
            }
        ]
    }

]

在action

//处理一个ajax请求
    public void child(){
        String st = ser.child();
        
        //异步传值
        HttpServletResponse res=ServletActionContext.getResponse();
        res.setContentType("text/html;charset=utf-8");
        try {
            PrintWriter pw=res.getWriter();
            pw.print(st);
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



对这个文档做拼接在业务处理层

public String child() {
        List<TtMenu> menulist=dao.find("from TtMenu where fatherMenuId=1");
        JSONArray all = new JSONArray();
        JSONObject first_menu = new JSONObject();
        
        for(TtMenu mn:menulist){//一级菜单
            
            first_menu.put("id", mn.getMenuId());
        
            //拼一级菜单
            JSONArray first_arr = new JSONArray();
            //只放一个json(一级菜单)对象,包含二级菜单
            
            JSONObject first= new JSONObject();
            first.put("text", mn.getMenuName());
            
            JSONArray second_arr = new JSONArray();
            //拼二级菜单
            JSONObject second= new JSONObject();
            
            for(TtMenu m:mn.getChildMenu()){//二级菜单
                second.put("id",m.getMenuId() );
                second.put("text", m.getMenuName());
                second.put("href", m.getMenuValue());
                
                second_arr.add(second);
            }
            
            first.put("items", second_arr);
            first_arr.add(first);
            first_menu.put("menu", first_arr);
            all.add(first_menu);
        }
        
        return all.toString();

    }


原创粉丝点击