通过JSONArray 生成2层节点的tree

long str = 0;
            JSONArray jsonArray = new JSONArray();
            Map mapArray = new HashMap();
            List list = new ArrayList();
List result = resultResource.getLacs();
Iterator o = result.iterator();
            while (o.hasNext())
            {
                OcCsmCell ocCsmCell = (OcCsmCell) o.next();
                Map mapObj = new HashMap();
                if (ocCsmCell.getLai() == str)
                {                  
                    mapObj.put("id", ocCsmCell.getLai());
                    mapObj.put("text", ocCsmCell.getLai());
                    mapObj.put("leaf", true);
                    mapObj.put("checked", false);
                    list.add(mapObj);
                }
                else
                {
                    if (list.size() > 0)
                    {
                        mapArray.put("children", list);
                        jsonArray.add(mapArray);
                    }
                    list = new ArrayList();
                    mapArray = new HashMap();
                    str = ocCsmCell.getLai();
                    mapArray.put("id", ocCsmCell.getCi());
                    mapArray.put("text", ocCsmCell.getCi());
                    mapArray.put("cls", "folder");
                    mapArray.put("checked", false);
                    Map mapTemp = new HashMap();
                    mapTemp.put("id", ocCsmCell.getCi());
                    mapTemp.put("text", ocCsmCell.getCi());
                    mapTemp.put("leaf", true);
                    mapTemp.put("checked", false);
                    list.add(mapTemp);
                }
                if (!o.hasNext())
                {
                    mapArray.put("children", list);
                    jsonArray.add(mapArray);
                }

你可能感兴趣的:(JsonArray)