将json格式多级菜单保存到数据库

作者: 城东那个小丑 | 来源:发表于2017-04-13 15:25 被阅读0次
    
        /**
         * 将json菜单保存到数据库
         *
         * @return
         */
        @RequestMapping("save/menu")
        @ResponseBody
        public Map<String, Object> menu() {
            String jsonStr = "{\n" +
                    "    \"data\": {\n" +
                    "        \"list\": [{\n" +
                    "                \"id\": 1,\n" +
                    "                \"url\": \"gzt\",\n" +
                    "                \"name\": \"工作台\",\n" +
                    "                \"iconfont\": \"\",\n" +
                    "                \"sub\": [{\n" +
                    "                    \"id\":11,\n" +
                    "                    \"url\": \"kfsh\",\n" +
                    "                    \"name\": \"客服售后\",\n" +
                    "                    \"iconfont\": \"\",\n" +
                    "                    \"sub\": [{\n" +
                    "                            \"id\": 1101,\n" +
                    "                            \"url\": \"kfsh/ddgl\",\n" +
                    "                            \"name\": \"订单管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"id\": 1102,\n" +
                    "                            \"url\": \"kfsh/pjgl\",\n" +
                    "                            \"name\": \"评价管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"id\": 1103,\n" +
                    "                            \"url\": \"kfsh/tscl\",\n" +
                    "                            \"name\": \"投诉处理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                        },\n" +
                    "                        {\n" +
                    "                            \"id\": 1104,\n" +
                    "                            \"url\": \"kfsh/tkgl\",\n" +
                    "                            \"name\": \"退款管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                        }\n" +
                    "                    ]\n" +
                    "                },\n" +
                    "                {\n" +
                    "                    \"id\": 12,\n" +
                    "                    \"url\": \"yggl\",\n" +
                    "                    \"name\": \"员工管理\",\n" +
                    "                    \"iconfont\": \"\",\n" +
                    "                    \"sub\": [{\n" +
                    "                            \"id\": 1201,\n" +
                    "                            \"url\": \"yggl/yglb\",\n" +
                    "                            \"name\": \"员工列表\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                          },\n" +
                    "                          {\n" +
                    "                            \"id\": 1203,\n" +
                    "                            \"url\": \"yggl/yggl\",\n" +
                    "                            \"name\": \"员工管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                          }\n" +
                    "                    ]\n" +
                    "                  },\n" +
                    "                  {\n" +
                    "                    \"id\": 13,\n" +
                    "                    \"url\": \"gzgl\",\n" +
                    "                    \"name\": \"工作管理\",\n" +
                    "                    \"iconfont\": \"\",\n" +
                    "                    \"sub\": [{\n" +
                    "                            \"id\": 1301,\n" +
                    "                            \"url\": \"gzgl/kjgl\",\n" +
                    "                            \"name\": \"卡卷管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                          },\n" +
                    "                            {\n" +
                    "                              \"id\": 1302,\n" +
                    "                              \"url\": \"gzgl/xtgg\",\n" +
                    "                              \"name\": \"系统公告\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1303,\n" +
                    "                              \"url\": \"gzgl/xtgug\",\n" +
                    "                              \"name\": \"系统广告\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            }\n" +
                    "                    ]\n" +
                    "                  },\n" +
                    "                  {\n" +
                    "                    \"id\": 14,\n" +
                    "                    \"url\": \"cwgl\",\n" +
                    "                    \"name\": \"财务管理\",\n" +
                    "                    \"iconfont\": \"\",\n" +
                    "                    \"sub\": [{\n" +
                    "                            \"id\": 1401,\n" +
                    "                            \"url\": \"cwgl/jsgl\",\n" +
                    "                            \"name\": \"结算管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                          },\n" +
                    "                            {\n" +
                    "                              \"id\": 1402,\n" +
                    "                              \"url\": \"cwgl/mdzw\",\n" +
                    "                              \"name\": \"门店账务\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1403,\n" +
                    "                              \"url\": \"cwgl/lszw\",\n" +
                    "                              \"name\": \"历史账务\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1404,\n" +
                    "                              \"url\": \"cwgl/cwfx\",\n" +
                    "                              \"name\": \"财务分析\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1405,\n" +
                    "                              \"url\": \"cwgl/ywfx\",\n" +
                    "                              \"name\": \"业务分析\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1406,\n" +
                    "                              \"url\": \"cwgl/pjfx\",\n" +
                    "                              \"name\": \"评价分析\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1407,\n" +
                    "                              \"url\": \"cwgl/yyfx\",\n" +
                    "                              \"name\": \"营业分析\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            }\n" +
                    "                    ]\n" +
                    "                  },\n" +
                    "                  {\n" +
                    "                    \"id\": 15,\n" +
                    "                    \"url\": \"qygl\",\n" +
                    "                    \"name\": \"区域管理\",\n" +
                    "                    \"iconfont\": \"\",\n" +
                    "                    \"sub\": [{\n" +
                    "                            \"id\": 1501,\n" +
                    "                            \"url\": \"qygl/mdgl\",\n" +
                    "                            \"name\": \"门店管理\",\n" +
                    "                            \"iconfont\": \"\"\n" +
                    "                          },\n" +
                    "                            {\n" +
                    "                              \"id\": 1502,\n" +
                    "                              \"url\": \"qygl/ddgl\",\n" +
                    "                              \"name\": \"订单管理\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1503,\n" +
                    "                              \"url\": \"qygl/cwgl\",\n" +
                    "                              \"name\": \"财务管理\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                              \"id\": 1504,\n" +
                    "                              \"url\": \"qygl/pjck\",\n" +
                    "                              \"name\": \"评价查看\",\n" +
                    "                              \"iconfont\": \"\"\n" +
                    "                            }\n" +
                    "                    ]\n" +
                    "                  }]\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\": 2,\n" +
                    "                \"url\": \"mdgl\",\n" +
                    "                \"name\": \"门店管理\",\n" +
                    "                \"iconfont\": \"\",\n" +
                    "                \"sub\": [{\n" +
                    "                        \"id\": 21,\n" +
                    "                        \"url\": \"mdgl/mdlb\",\n" +
                    "                        \"name\": \"门店列表\",\n" +
                    "                        \"iconfont\": \"\"\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 22,\n" +
                    "                        \"url\": \"mdgl/ywgl\",\n" +
                    "                        \"name\": \"业务管理\",\n" +
                    "                        \"iconfont\": \"\"\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 23,\n" +
                    "                        \"url\": \"mdgl/tcgl\",\n" +
                    "                        \"name\": \"套餐管理\",\n" +
                    "                        \"iconfont\": \"\"\n" +
                    "\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 24,\n" +
                    "                        \"url\": \"mdgl/mdzk\",\n" +
                    "                        \"name\": \"门店折扣\",\n" +
                    "                        \"iconfont\": \"\"\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 25,\n" +
                    "                        \"url\": \"mdgl/czsz\",\n" +
                    "                        \"name\": \"充值设置\",\n" +
                    "                        \"iconfont\": \"\"\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 26,\n" +
                    "                        \"url\": \"mdgl/spgl\",\n" +
                    "                        \"name\": \"商品管理\",\n" +
                    "                        \"iconfont\": \"\"\n" +
                    "\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                      \"id\": 27,\n" +
                    "                      \"url\": \"mdgl/qygl\",\n" +
                    "                      \"name\": \"区域管理\",\n" +
                    "                      \"iconfont\": \"\"\n" +
                    "\n" +
                    "                    }\n" +
                    "                ]\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\": 3,\n" +
                    "                \"url\": \"hygl\",\n" +
                    "                \"name\": \"会员管理\",\n" +
                    "                \"iconfont\": \"\",\n" +
                    "                \"sub\": [{\n" +
                    "                    \"id\": 31,\n" +
                    "                    \"url\": \"hygl/hylb\",\n" +
                    "                    \"name\": \"会员列表\",\n" +
                    "                    \"iconfont\": \"\"\n" +
                    "                  },\n" +
                    "                  {\n" +
                    "                    \"id\": 32,\n" +
                    "                    \"url\": \"hygl/hydj\",\n" +
                    "                    \"name\": \"会员等级\",\n" +
                    "                    \"iconfont\": \"\"\n" +
                    "                  },\n" +
                    "                  {\n" +
                    "                    \"id\": 33,\n" +
                    "                    \"url\": \"hygl/hytj\",\n" +
                    "                    \"name\": \"会员统计\",\n" +
                    "                    \"iconfont\": \"\"\n" +
                    "                  }]\n" +
                    "            },\n" +
                    "            {\n" +
                    "                \"id\": 4,\n" +
                    "                \"url\": \"xtgl\",\n" +
                    "                \"name\": \"系统管理\",\n" +
                    "                \"iconfont\": \"\",\n" +
                    "                \"sub\": [{\n" +
                    "                        \"id\": 41,\n" +
                    "                        \"url\": \"xtgl/xtgl\",\n" +
                    "                        \"name\": \"系统管理\",\n" +
                    "                        \"iconfont\": \"\",\n" +
                    "                        \"sub\": [\n" +
                    "                            {\n" +
                    "                                \"id\": 4102,\n" +
                    "                                \"url\": \"xtgl/xtrz\",\n" +
                    "                                \"name\": \"系统日志\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"id\": 4103,\n" +
                    "                                \"url\": \"xtgl/cdgl\",\n" +
                    "                                \"name\": \"菜单管理\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"id\": 4104,\n" +
                    "                                \"url\": \"xtgl/pbc\",\n" +
                    "                                \"name\": \"屏蔽词\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                            }\n" +
                    "                        ]\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 42,\n" +
                    "                        \"url\": \"qxgl\",\n" +
                    "                        \"name\": \"权限管理\",\n" +
                    "                        \"iconfont\": \"\",\n" +
                    "                        \"sub\": [{\n" +
                    "                                \"id\": 4201,\n" +
                    "                                \"url\": \"qxgl/jsgl\",\n" +
                    "                                \"name\": \"角色管理\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"id\": 4202,\n" +
                    "                                \"url\": \"qxgl/qxsz\",\n" +
                    "                                \"name\": \"权限设置\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                            },\n" +
                    "                            {\n" +
                    "                                \"id\": 4203,\n" +
                    "                                \"url\": \"qxgl/gly\",\n" +
                    "                                \"name\": \"管理员\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                            }\n" +
                    "                        ]\n" +
                    "                    },\n" +
                    "                    {\n" +
                    "                        \"id\": 43,\n" +
                    "                        \"url\": \"xtsj\",\n" +
                    "                        \"name\": \"系统数据\",\n" +
                    "                        \"iconfont\": \"\",\n" +
                    "                        \"sub\": [{\n" +
                    "                                \"id\": 4301,\n" +
                    "                                \"url\": \"xtsj/xtyw\",\n" +
                    "                                \"name\": \"系统业务\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                              },\n" +
                    "                                {\n" +
                    "                                  \"id\": 4302,\n" +
                    "                                  \"url\": \"xtsj/ywfl\",\n" +
                    "                                  \"name\": \"业务分类\",\n" +
                    "                                  \"iconfont\": \"\"\n" +
                    "                                },\n" +
                    "                                {\n" +
                    "                                  \"id\": 4303,\n" +
                    "                                  \"url\": \"xtsj/cxcx\",\n" +
                    "                                  \"name\": \"车型车系\",\n" +
                    "                                  \"iconfont\": \"\"\n" +
                    "                                },\n" +
                    "                                {\n" +
                    "                                \"id\": 4101,\n" +
                    "                                \"url\": \"xtgl/dictionary\",\n" +
                    "                                \"name\": \"数据字典\",\n" +
                    "                                \"iconfont\": \"\"\n" +
                    "                             }\n" +
                    "                                \n" +
                    "                        ]\n" +
                    "                    }\n" +
                    "                ]\n" +
                    "            }\n" +
                    "        ]\n" +
                    "    },\n" +
                    "    \"status\": 200\n" +
                    "}";
            JSONObject jsStr = JSONObject.parseObject(jsonStr);
            Map<String, Object> map = (Map<String, Object>) jsStr.get("data");
            List<Object> list = (List<Object>) map.get("list");
            try {
                for (Object menu1 : list) {
                    SysMenu menu = new SysMenu();
                    JSONObject jsonObject = JSONObject.parseObject(menu1.toString());
                    System.out.println("" + jsonObject.get("url").toString());
                    menu.setMenuUrl(jsonObject.get("url").toString());
                    menu.setMenuIcon(jsonObject.get("iconfont").toString());
                    menu.setMenuName(jsonObject.get("name").toString());
                    menu.setParentId("0");
                    //保存
                    SysMenu sysMenu = sysMenuService.saveEntity(menu);//保存save(menu)
                    List<Object> list1 = (List<Object>) jsonObject.get("sub");
                    if (list1.size() > 0) {
                        for (Object menu2 : list1) {
                            JSONObject jsonObject1 = JSONObject.parseObject(menu2.toString());
                            System.out.println("    " + jsonObject1.get("url").toString());
                            SysMenu m1 = new SysMenu();
                            m1.setMenuUrl(jsonObject1.get("url").toString());
                            m1.setMenuIcon(jsonObject1.get("iconfont").toString());
                            m1.setMenuName(jsonObject1.get("name").toString());
                            m1.setParentId(sysMenu.getMenuId() + "");
                            //保存
    
                            SysMenu sysMenu1 = sysMenuService.saveEntity(m1);//保存save(m1)
    
    
                            List<Object> list3 = (List<Object>) jsonObject1.get("sub");
                            if (list3 != null) {
                                for (Object menu3 : list3) {
                                    JSONObject jsonObject2 = JSONObject.parseObject(menu3.toString());
                                    System.out.println("        " + jsonObject2.get("url").toString());
    
                                    SysMenu m2 = new SysMenu();
                                    m2.setMenuUrl(jsonObject2.get("url").toString());
                                    m2.setMenuIcon(jsonObject2.get("iconfont").toString());
                                    m2.setMenuName(jsonObject2.get("name").toString());
                                    m2.setParentId(sysMenu1.getMenuId() + "");
                                    sysMenuService.saveEntity(m2);
                                }
                            }
                        }
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
    

    相关文章

      网友评论

        本文标题:将json格式多级菜单保存到数据库

        本文链接:https://www.haomeiwen.com/subject/nzvbattx.html