Java 用于将JSON转换为映射的递归函数

Java 用于将JSON转换为映射的递归函数,java,json,recursion,map,Java,Json,Recursion,Map,我必须制作一个JSONlike [ { title=X, folder=true, key=1, children=[ { folder=true, title=X, key=2, children=[ { folder=true, title=Y, key=3

我必须制作一个
JSON
like

[
  {
    title=X,
    folder=true,
    key=1,
    children=[
        {
        folder=true,
        title=X,
        key=2,
        children=[
            {
            folder=true,
            title=Y,
            key=3
            },
            {
            folder=true,
            title=VH,
            key=4
            },
            {
            folder=true,
            title=Tell,
            key=7
            }
        ]

        },
        {
        folder=true,
        title=X8,
        key=5,
        children=[
            {
            folder=true,
            title=Crah,
            key=6
            }
        ]
        }
    ]
  }
]
我必须将我的结构保存在
HashMap

{1={parentID=NULL, name=Car}, 2={parentID=1, name=X}, 3={parentID=2, name=Y}, 4={parentID=2, name=VH}, 5={parentID=1, name=X8}, 6={parentID=5, name=Crah}, 7={parentID=2, name=Tell}}
这是我现在的代码:

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class projects {

    public HashMap<Integer, HashMap<String, String>> ld = new HashMap<Integer, HashMap<String, String>>();
    public HashMap child = new HashMap();
    ArrayList alfinal = new ArrayList();

    @SuppressWarnings("uncheck")
    public static  void main(String[] args) {
        // TODO Auto-generated method stub
        projects po =new projects();
        String[] arname = {"Car","X","Y","VH","X8","Crah","Tell"};
        String[] arparent = {"NULL","1","2","2","1","5","2"};
        for (int ii = 1; ii < 8; ii++){
            HashMap hm1 = new HashMap();
            hm1.put("name",arname[ii-1]);
            hm1.put("parentID",arparent[ii-1]);
            po.ld.put(ii, hm1);
        }


        int key=1;
        int depth=2;
        Boolean cdepth = true;
        ArrayList tmp = new ArrayList();
        po.getNodeList(key,depth,tmp);
        System.out.println(tmp);

    }


    public  ArrayList getNodeList(int key,int depth,ArrayList all)
    {
        ArrayList out = new ArrayList();
        out=all;
        HashMap childs = new HashMap();
        ArrayList tmp = new ArrayList();
        if(depth>0)
        {
            childs=getchildes(key);
            tmp=(ArrayList) childs.get(Integer.toString(key));
            depth--;
        }

        if(tmp !=null)
        {


            ArrayList cchin = new ArrayList();
            for (int i = 0; i < tmp.size(); i++) {
                Integer ckey=(Integer) tmp.get(i);
                if(depth>0)
                {
                    ArrayList arrt = new ArrayList();
                    arrt=getNodeList( ckey, depth,out);
                    HashMap hmouti = new HashMap<String, String>();
                    hmouti.put("key", ckey);
                    hmouti.put("title", ld.get(ckey).get("name"));
                    hmouti.put("folder", "true");
                    hmouti.put("children",arrt);
                    out.add(hmouti);

                }

                HashMap hmouti = new HashMap<String, String>();
                hmouti.put("key", ckey);
                hmouti.put("title", ld.get(ckey).get("name"));
                hmouti.put("folder", "true");
                cchin.add(hmouti);


            }

            return cchin;

        }


        return out;


    }



    public HashMap getchildes(int id)
    {
        HashMap child = new HashMap();
        Iterator iter = ld.keySet().iterator();
        while(iter.hasNext()) {
            Integer key = (Integer)iter.next();

            String parentID=ld.get(key).get("parentID");
            if(parentID.equals("NULL")){
                continue;
            }
            else
            {
                if(Integer.toString(id).equals(parentID))
                {
                if(child.get(parentID)!= null)
                {
                    ArrayList tmp = new ArrayList();
                    tmp=(ArrayList) child.get(parentID);
                    tmp.add(key);
                    child.put(parentID, tmp);

                }
                else
                {
                    ArrayList tmp = new ArrayList();
                    tmp.add(key);
                    child.put(parentID, tmp);

                }
                }
            }

        }
        return child;

    }



}
封装测试;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
公共类项目{
public HashMap ld=new HashMap();
public HashMap child=new HashMap();
ArrayList alfinal=新的ArrayList();
@抑制警告(“取消选中”)
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
项目po=新项目();
字符串[]arname={“Car”、“X”、“Y”、“VH”、“X8”、“Crah”、“Tell”};
字符串[]arparent={“NULL”、“1”、“2”、“2”、“1”、“5”、“2”};
对于(int ii=1;ii<8;ii++){
HashMap hm1=新的HashMap();
hm1.put(“名称”,arname[ii-1]);
hm1.put(“parentID”,arparent[ii-1]);
购买力平价(ii,hm1);
}
int键=1;
int深度=2;
布尔值cdepth=true;
ArrayList tmp=新的ArrayList();
po.getNodeList(键、深度、tmp);
系统输出打印LN(tmp);
}
公共ArrayList getNodeList(int-key、int-depth、ArrayList-all)
{
ArrayList out=新的ArrayList();
out=全部;
HashMap childs=新的HashMap();
ArrayList tmp=新的ArrayList();
如果(深度>0)
{
childs=getchildes(键);
tmp=(ArrayList)childs.get(Integer.toString(key));
深度--;
}
如果(tmp!=null)
{
ArrayList cchin=新的ArrayList();
对于(int i=0;i0)
{
ArrayList arrt=新的ArrayList();
arrt=getNodeList(ckey,深度,向外);
HashMap hmouti=新的HashMap();
hmouti.put(“键”,ckey);
hmouti.put(“title”,ld.get(ckey.get(“name”));
hmouti.put(“文件夹”、“真”);
hmouti.put(“儿童”,arrt);
out.add(hmouti);
}
HashMap hmouti=新的HashMap();
hmouti.put(“键”,ckey);
hmouti.put(“title”,ld.get(ckey.get(“name”));
hmouti.put(“文件夹”、“真”);
cchin.add(hmouti);
}
返回cchin;
}
返回;
}
公共HashMap getchildes(int-id)
{
HashMap child=新的HashMap();
迭代器iter=ld.keySet().Iterator();
while(iter.hasNext()){
整数键=(整数)iter.next();
字符串parentID=ld.get(key.get)(“parentID”);
if(parentID.equals(“NULL”)){
继续;
}
其他的
{
if(Integer.toString(id).equals(parentID))
{
if(child.get(parentID)!=null)
{
ArrayList tmp=新的ArrayList();
tmp=(ArrayList)child.get(parentID);
tmp.add(键);
child.put(parentID,tmp);
}
其他的
{
ArrayList tmp=新的ArrayList();
tmp.add(键);
child.put(parentID,tmp);
}
}
}
}
返回儿童;
}
}
我已经编写了一个递归函数,但它缺少具有键的父文件夹。。。我将键1赋给getNodeList,它返回键1的子项,但不包括父项本身(即键1)


请帮助我确定错误在哪里。

请查看按以下方式修改类是否有助于获得所需的输出

public class projects  {

    public HashMap<Integer, HashMap<String, String>> ld = new HashMap<Integer, HashMap<String, String>>();
    public HashMap child = new HashMap();
    ArrayList alfinal = new ArrayList();

    @SuppressWarnings("uncheck")
    public static  void main(String[] args) {
        // TODO Auto-generated method stub
        projects  po =new projects ();
        String[] arname = {"Car","X","Y","VH","X8","Crah","Tell"};
        String[] arparent = {"NULL","1","2","2","1","5","2"};
        for (int ii = 1; ii < 8; ii++){
            HashMap hm1 = new HashMap();
            hm1.put("name",arname[ii-1]);
            hm1.put("parentID",arparent[ii-1]);
            po.ld.put(ii, hm1);
        }


        int key=1;
        int depth=2;
        Boolean cdepth = true;
        ArrayList tmp = new ArrayList();
        po.getNodeList(key,depth,tmp);
        System.out.println(tmp);

    }


    public  ArrayList getNodeList(int key,int depth,ArrayList all)
    {
        ArrayList out = new ArrayList();
        out=all;
        HashMap childs = new HashMap();
        ArrayList tmp = new ArrayList();
        if(depth>0)
        {
            childs=getchildes(key);
            tmp=(ArrayList) childs.get(Integer.toString(key));
            depth--;
        }
        System.out.println(" tmp  "+tmp);
        if(tmp !=null)
        {


            ArrayList cchin = new ArrayList();
            for (int i = 0; i < tmp.size(); i++) {
                Integer ckey=(Integer) tmp.get(i);

                if(depth>0)
                {
                    ArrayList arrt = new ArrayList();
                    arrt=getNodeList( ckey, depth,out);
                    HashMap hmouti = new HashMap<String, String>();
                    hmouti.put("key", ckey);
                    hmouti.put("title", ld.get(ckey).get("name"));
                    hmouti.put("folder", "true");
                    hmouti.put("children",arrt);
                    out.add(hmouti);

                }

                HashMap hmouti = new HashMap<String, String>();
                hmouti.put("key", ckey);
                hmouti.put("title", ld.get(ckey).get("name"));
                hmouti.put("folder", "true");
                cchin.add(hmouti);


            }

            return cchin;

        }


        return out;


    }



    public HashMap getchildes(int id)
    {
        HashMap child = new HashMap();
        Iterator iter = ld.keySet().iterator();
        while(iter.hasNext()) {
            Integer key = (Integer)iter.next();

            String parentID=ld.get(key).get("parentID");
            if(parentID.equals("NULL")){
                if(id==1)
                {

                    ArrayList tmp = new ArrayList();
                    tmp.add(key);
                    child.put(Integer.toString(id), tmp);

                } 

            }
            else
            { 
                if(Integer.toString(id).equals(parentID))
                {
                    if(child.get(parentID)!= null)
                    {
                        ArrayList tmp = new ArrayList();
                        tmp=(ArrayList) child.get(parentID);
                        tmp.add(key);
                        child.put(parentID, tmp);

                    }
                    else
                    {
                        ArrayList tmp = new ArrayList();
                        tmp.add(key);
                        child.put(parentID, tmp);

                    }
                }
            }

        }
        return child;

    }

}
公共类项目{
public HashMap ld=new HashMap();
public HashMap child=new HashMap();
ArrayList alfinal=新的ArrayList();
@抑制警告(“取消选中”)
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
项目po=新项目();
字符串[]arname={“Car”、“X”、“Y”、“VH”、“X8”、“Crah”、“Tell”};
字符串[]arparent={“NULL”、“1”、“2”、“2”、“1”、“5”、“2”};
对于(int ii=1;ii<8;ii++){
HashMap hm1=新的HashMap();
hm1.put(“名称”,arname[ii-1]);
hm1.put(“parentID”,arparent[ii-1]);
购买力平价(ii,hm1);
}
int键=1;
int深度=2;
布尔值cdepth=true;
ArrayList tmp=新的ArrayList();
po.getNodeList(键、深度、tmp);
系统输出打印LN(tmp);
}
公共ArrayList getNodeList(int-key、int-depth、ArrayList-all)
{
ArrayList out=新的ArrayList();
out=全部;
HashMap childs=新的HashMap();
ArrayList tmp=新的ArrayList();
如果(深度>0)
{
childs=getchildes(键);
tmp=(ArrayList)childs.get(Integer.toString(key));
深度--;
}
系统输出打印项次(“tmp”+tmp);
如果(tmp!=null)
{
ArrayList cchin=新的ArrayList();
对于(int i=0;i0)
{
ArrayList arrt=新的ArrayList();
arrt=getNodeList(ckey,深度,向外);
HashMap hmouti=新的HashMap();
hmouti.put(“键”,ckey);
hmouti.put(“title”,ld.get(ckey.get(“name”));
hmouti.put(“文件夹”、“真”);
hmouti.put(“儿童”,arrt);
out.add(hmouti);
}
HashMap hmouti=新的HashMap();
hmouti.put(“键”,ckey);
hmouti.put(“title”,ld.get(ckey.get(“name”));
hmouti.put(“文件夹”、“真”);
cchin.add(hmouti);
}
回归cchin
public static String toJson(Map.Entry<String, Map<String, String>> entry, Map<String, Map<String, String>> map)
{
  StringBuilder json = new StringBuilder();

  json.append("{")
    .append("\"title\":\"").append(entry.getValue().get("title")).append("\", ")
    .append("\"key\":\"").append(entry.getKey()).append("\", ");

  Collection<Map.Entry<String, Map<String, String>>> children = findChildren(entry.getKey(), map);

  if (!children.isEmpty())
  {
    json.append("\"children\": [ ");

    for (Map.Entry<String, Map<String, String>> childEntry : children)
    {
      json.append(toJson(childEntry, map)).append(", ");
    }

    json.replace(json.length() - 2, json.length(), "").append(" ], ");
  }

  json.replace(json.length() - 2, json.length(), "").append(" }");

  return json.toString();
}
private static Collection<Map.Entry<String, Map<String, String>>> findChildren(String key,  Map<String,Map<String, String>> map)
{
  List<Map.Entry<String, Map<String, String>>> children = new ArrayList<>();

  for (Map.Entry<String, Map<String, String>> entry : map.entrySet())
  {
    if (key.equals(entry.getValue().get("parentId")))
    {
      children.add(entry);
    }
  }

  return children;
}
Map<String, Map<String, String>> map = new HashMap<>();
map.put("1", createEntity(null, "car"));
map.put("2", createEntity("1", "X"));
map.put("3", createEntity("2", "Y"));
map.put("4", createEntity("2", "VH"));
map.put("5", createEntity("1", "X8"));
map.put("6", createEntity("5", "Crah"));
map.put("7", createEntity("2", "Tell"));

System.out.println(toJson(findEntry(map, "1"), map));
{
  "title":"car",
  "key":"1", 
  "children": [ 
    {
      "title":"X", 
      "key":"2", 
      "children": [ 
        {
          "title":"Y", 
          "key":"3" 
        }, 
        {
          "title":"VH", 
          "key":"4" 
        }, 
        {
          "title":"Tell", 
          "key":"7" 
        } 
      ] 
    }, 
    {
      "title":"X8", 
      "key":"5", 
      "children": [ 
        { 
          "title":"Crah", 
          "key":"6" 
        } 
      ] 
    } 
  ] 
}