Java -从JSON获取ID列表,深度可变

ecfdbz9o  于 2023-06-25  发布在  Java
关注(0)|答案(2)|浏览(139)

我有一个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);
            }
            
        }
    }

那么有没有一种方法来构建那些列表,并修改我的例程来做它或其他东西(另一个例程,……)
谢谢

tyg4sfes

tyg4sfes1#

我会建议一个递归的解决方案来跟踪当前的子树:

publi List<List<String>> getSubtrees(JsonNode childrenNode, List<String> currentSubtree) {
    ArrayList<List<String>> subtrees = new ArrayList<>();

    if (childrenNode.isEmpty()) {
        subtrees.add(currentSubtree);
    } else {
        for (JsonNode node : childrenNode) {
            String id = node.get("id").asText();
            JsonNode children = node.get("children");
            List<String> childSubtree = new ArrayList<>(currentSubtree);
            childSubtree.add(id);

            subtrees.addAll(getSubtrees(children, childSubtree));
        }
    }

    return subtrees;
}

称之为

List<List<String>> subtrees = getSubtrees(children, List.of());

其中children是顶级子节点的数组节点,在您的示例中,它是arbre -> children json节点。这是一个例子,请注意类型/null检查。

p1iqtdky

p1iqtdky2#

用(接受的)答案中的解决方案(https://stackoverflow.com/a/76456855/14707253
我做了一些修改,但解决方案本身工作,所以这里是最终代码:功能

public List<List<String>> getSubtrees(JsonNode childrenNode, List<String> currentSubtree) {
        ArrayList<List<String>> subtrees = new ArrayList<>();

        if (childrenNode.size() == 0) {
            subtrees.add(currentSubtree);
        } else {
            for (int i = 0; i < childrenNode.size(); i++) {
                //System.out.println(childrenNode.get(i).toString());
                String id = childrenNode.get(i).get("id").toString();
                //System.out.println(id);
                JsonNode children = childrenNode.get(i).get("children");
                List<String> childSubtree = new ArrayList<>(currentSubtree);
                childSubtree.add(id);
                //System.out.println(childSubtree.toString());
                subtrees.addAll(getSubtrees(children, childSubtree));
            }
        }
        //System.out.println(subtrees.size());
        return subtrees;
    }

和函数的调用:

public List<List<String>> preProcessGetSubtrees(String tree) throws JsonProcessingException, IOException {
        if(!isNullorEmpty(tree)) {
            List<String> currentSubtree = new ArrayList<>();
            ObjectMapper objMapper = new ObjectMapper();
            System.out.println("=========================== PLANETE TREE PROCESSING ========================");
            //List<List<String>> subtrees = 
            List<List<String>> subtrees = getSubtrees(objMapper.readTree(tree).get("arbre").get("children"), currentSubtree);
            System.out.println("=========================== PLANETE TREE PROCESSED ========================");
            return subtrees;
        } else {
            System.out.println("============================================================================");
            System.out.println("==================== WARNING THE TREE IS NOT VALID =========================");
            System.out.println("============================================================================");
            return null;
        }

相关问题