我有一个JSON文件,深度波动,每个节点(深度不同)。所以它就像一棵通用树:
下面是一个json示例(不是真实的的json,而是一个mock文件,但结构保持不变):
{
"arbre" : {
"children" : [
{
"id" : 1,
"children" : [
{
"id" : 3,
"children" : [
{
"id" : 6,
"children" : []
}
]
},
{
"id" : 4,
"children": []
},
{
"id" : 5,
"children": []
}
]
},
{
"id" : 2,
"children" : [
{
"id" : 7,
"children" :
[
{
"id" : 8,
"children" : [
{
"id" : 9,
"children" : []
}
]
},
{
"id" : 10,
"children" : [
{
"id" : 11,
"children" : []
}
]
}
]
}
]
}
]
}
}
我试图为每个子树构建多个id列表,例如,我需要:
- 一、三、六
- 一、四
- 一、五
- 二、七、八、九
- 二、七、十、十一
我以前开发了一个使用Jackson遍历整个树(递归)的例程,不知道我是否可以将其应用于这个用例。
这就是:
/**
* getJSONNode: not return value, recursive function that will browse (uses functionalities and objects of the com.fasterxml.jackson library)
*
*
* {talendTypes} String, JsonNode
*
* {Category} User Defined
*
* {param} JsonNode(jNode) input: the function will check if the node is an array of object and will process and keep iterate until it reaches the bottom of the tree
* {param} string(filepath) input: The string is a path to a directory where the temp files will be stored
*
* {example} getJSONNode(jNode, "/test/") # Files will be generated in the directory /test/
*/
public static void getJSONNode(JsonNode jNode, String filepath) throws IOException {
//System.out.println("Get Node");
JsonNode tempNoded = jNode.get("children");
if(tempNoded.isArray()) {
for(int i = 0; i < tempNoded.size(); i++) {
System.out.println("Name : " + tempNoded.get(i).get("name"));
System.out.println("Title : " + tempNoded.get(i).get("title"));
System.out.println("Title : " + tempNoded.get(i).get("id"));
System.out.println("================== Writing file ===================");
String id = tempNoded.get(i).get("id").toString();
String name = tempNoded.get(i).get("name").toString();
String title = tempNoded.get(i).get("title").toString();
String color = tempNoded.get(i).get("color").toString();
String order = tempNoded.get(i).get("order").toString();
String parentId = tempNoded.get(i).get("parentId").toString();
String persons = tempNoded.get(i).get("persons").toString();
String profils = tempNoded.get(i).get("profils").toString();
String paramNoeud = tempNoded.get(i).get("paramNoeud").toString();
String description = tempNoded.get(i).get("description").toString();
// disable not present in all dimensions so not usable
//String disabled = tempNoded.get(i).get("disabled").toString();
String oldId = tempNoded.get(i).get("oldId").toString();
String str = id + ";" + name + ";" + title + ";" + color + ";" + order + ";" + parentId + ";" + persons + ";" + profils + ";" + paramNoeud + ";" + description + ";" + oldId;
byte[] strToBytes = str.getBytes();
String filename = (name + "_" + order + ".csv").replaceAll("\"", "");
File file = new File(filepath + filename);
file.getParentFile().mkdirs();
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(filepath + filename);
outputStream.write(strToBytes);
outputStream.close();
getJSONNode(tempNoded.get(i), filepath);
}
}
}
那么有没有一种方法来构建那些列表,并修改我的例程来做它或其他东西(另一个例程,……)
谢谢
2条答案
按热度按时间tyg4sfes1#
我会建议一个递归的解决方案来跟踪当前的子树:
称之为
其中
children
是顶级子节点的数组节点,在您的示例中,它是arbre -> children
json节点。这是一个例子,请注意类型/null检查。p1iqtdky2#
用(接受的)答案中的解决方案(https://stackoverflow.com/a/76456855/14707253)
我做了一些修改,但解决方案本身工作,所以这里是最终代码:功能
和函数的调用: