public static void getChildrenList(List<JSONObject> list,JSONObject pJo){ List<JSONObject> retList=new ArrayList<JSONObject>(); for(JSONObject jo:list){ if(jo.getString("pid").equals(pJo.getString("id"))){ retList.add(jo); jo.put("children", getChildrenList(list, jo)); } } //return retList; pJo.put("children",retList); }
第一种,方法循环一次,比较耗费内存,不建议使用
public static void main(String[] args) { //拼装数据 List<JSONObject> list=new ArrayList<JSONObject>(); list.add(JSONObject.parseObject("{id:'1',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'2',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'3',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'1-1',name:'n1-1',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-1',name:'n2-1',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-1',name:'n3-1',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-2',name:'n1-2',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-2',name:'n2-2',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-2',name:'n3-2',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-3',name:'n1-3',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-3',name:'n2-3',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-3',name:'n3-3',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-1-1',name:'n1-1-1',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-2',name:'n1-1-2',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-3',name:'n1-1-3',pid:'1-1'}")); //递归 根节点 JSONObject tJo=JSONObject.parseObject("{id:'-1',name:'根节点'}"); //tJo.put("children",getChildrenList(list, tJo)); System.out.println(tJo.toJSONString()); //一次循环 方法,比较好内存不建议使用 Map<String,List<JSONObject>> map1=new HashMap<String, List<JSONObject>>(); for(JSONObject jo:list){ String pid=jo.getString("pid"); String id=jo.getString("id"); if(!map1.containsKey(pid)){//不存在 map1.put(pid, new ArrayList<JSONObject>()); } map1.get(pid).add(jo); if(!map1.containsKey(id)){ map1.put(id, new ArrayList<JSONObject>()); } jo.put("children", map1.get(id)); } JSONObject tJo2=JSONObject.parseObject("{id:'-1',name:'根节点'}"); tJo2.put("children", map1.get("-1")); System.out.println(tJo2.toJSONString()); }
第二种方法,两次循环,建议使用,节省内存
public static void main(String[] args) { //拼装数据 List<JSONObject> list=new ArrayList<JSONObject>(); list.add(JSONObject.parseObject("{id:'1',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'2',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'3',name:'n1',pid:'-1'}")); list.add(JSONObject.parseObject("{id:'1-1',name:'n1-1',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-1',name:'n2-1',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-1',name:'n3-1',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-2',name:'n1-2',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-2',name:'n2-2',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-2',name:'n3-2',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-3',name:'n1-3',pid:'1'}")); list.add(JSONObject.parseObject("{id:'2-3',name:'n2-3',pid:'2'}")); list.add(JSONObject.parseObject("{id:'3-3',name:'n3-3',pid:'3'}")); list.add(JSONObject.parseObject("{id:'1-1-1',name:'n1-1-1',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-2',name:'n1-1-2',pid:'1-1'}")); list.add(JSONObject.parseObject("{id:'1-1-3',name:'n1-1-3',pid:'1-1'}")); //递归 JSONObject tJo=JSONObject.parseObject("{id:'-1',name:'根节点'}"); //tJo.put("children",getChildrenList(list, tJo)); System.out.println(tJo.toJSONString()); //两次循环 方法,建议使用 Map<String,List<JSONObject>> map=new HashMap<String, List<JSONObject>>(); for(JSONObject jo:list){ String pid=jo.getString("pid"); if(!map.containsKey(pid)){//不存在 map.put(pid, new ArrayList<JSONObject>()); } map.get(pid).add(jo); } for(JSONObject jo:list){ String id=jo.getString("id"); if(map.containsKey(id)){ jo.put("children", map.get(id)); } } JSONObject tJo1=JSONObject.parseObject("{id:'-1',name:'根节点'}"); tJo1.put("children", map.get("-1")); System.out.println(tJo1.toJSONString()); }