java拼三级菜单树json

            t6 = Test6.class.newInstance();
            List<Menu> mList = t6.getMenu();
            Menu m = null;           
            List<Menu> m1 = new ArrayList<Menu>();
            List<Menu> m2 = new ArrayList<Menu>();
            List<Menu> m3 = new ArrayList<Menu>();
            for (int i = 0; i < mList.size(); i++)
            {
                m = mList.get(i);
                if("0".equals(m.getPid())){//表示第一级
                    m1.add(m);
                    mList.remove(i);
                    i--;
                }
                if(m.getPid().length() == 3){//表示二级
                    m2.add(m);
                    mList.remove(i);
                    i--;
                }
                if(m.getPid().length() == 5){//表示三级
                    m3.add(m);
                    mList.remove(i);
                    i--;
                }
            }
            System.out.println("/////////////////////////");
            //拼接菜单树的三级json
//            [{'id':901'name':学校1,'childNotes':[{'id':90101'name':学校1班级1,'childNotes':[{'id':9010101'name':学校1班级1组1},{'id':9010102'name':学校1班级1组2}]},
//                                               {'id':90102'name':学校1班级2,'childNotes':[]}]},
//             {'id':902'name':学校2,'childNotes':[{'id':90201'name':学校2班级1,'childNotes':[]},{'id':90202'name':学校2班级2,'childNotes':[]}]}]
            StringBuilder sb = new StringBuilder("[");
            Menu mt1 = null;
            Menu mt2 = null;
            Menu mt3 = null;
            for (int i = 0; i < m1.size(); i++)
            {
                mt1 = m1.get(i);
                sb.append("{");
                sb.append("'id':"+mt1.getMid());
                sb.append("'name':"+mt1.getmName());
                sb.append(",'childNotes':[");
                for (int j = 0; j < m2.size(); j++)
                {
                    mt2 = m2.get(j);
                    if(mt1.getMid().equals(mt2.getPid())){//如果二级的父id与一级id一样
                        sb.append("{");
                        sb.append("'id':"+mt2.getMid());
                        sb.append("'name':"+mt2.getmName());
                        sb.append(",'childNotes':[");
                        for (int j2 = 0; j2 < m3.size(); j2++)
                        {
                            mt3 = m3.get(j2);
                            if(mt2.getMid().equals(mt3.getPid())){
                                sb.append("{");
                                sb.append("'id':"+mt3.getMid());
                                sb.append("'name':"+mt3.getmName());
                                sb.append("},");
                            }
                        }
                        if(regexCheckStr(".*,",sb.toString())){
                            sb.replace(sb.length()-1, sb.length(), "");
                        }
                        sb.append("]");
                        sb.append("},");
                    }
                }
                if(regexCheckStr(".*,",sb.toString())){
                    sb.replace(sb.length()-1, sb.length(), "");
                }
                sb.append("]");
                sb.append("},");
            }
            //[{id:'',name:'',ch:[{id:name:ch:[{}]},{}]},
//          {id:'',name:'',ch:[]}]
            if(regexCheckStr(".*,",sb.toString())){//是否以,结束,是就将,替换为空
                sb.replace(sb.length()-1, sb.length(), "");
            }
            sb.append("]");
            System.out.println(sb.toString());    

你可能感兴趣的:(java,json,J#)