gpt4 book ai didi

java - 从 JSON 转换为 Map 的递归函数

转载 作者:行者123 更新时间:2023-12-01 13:09:36 26 4
gpt4 key购买 nike

我必须制作一个JSON,例如

[
{
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;

}



}

我编写了一个递归函数,但它缺少带有键的父文件夹...我将键 1 赋予 getNodeList ,它返回键 1 的子级,但它不包括父级本身(即键 1 )。

请帮我找出错误所在。

最佳答案

有几件事

  • 您的示例 JSON 无效 - 请参阅 json.org .
  • 恕我直言,您的代码很难遵循。

这是我如何使用递归函数来做到这一点:

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();
}

注意:不检查循环引用。

这段代码使用了一个方法findChildren,它只返回给定条目的子条目的Collection:

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));

其中 findEntry 方法从 map 返回给定键的条目,在本例中为 “1”

这将打印(添加缩进以提高可读性):

{
"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"
}
]
}
]
}

关于java - 从 JSON 转换为 Map 的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22986975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com