Java 如何实现json树结构格式

Java 如何实现json树结构格式,java,json,tree,Java,Json,Tree,我需要将excel中的数据转换为树结构。因为我将使用EXTJS树面板,所以我需要将java对象存储为JSON格式: Logic ---Propositional Logic ---Predicate Logic ---Modal Logic -------Stit logic -------Doxastic logic ---Temporal logic 我从excel工作表中读取数据,并使用multimap将每个键与多个值关联起来。 存储在multi-map中后的我的i/p:[Logic=>

我需要将excel中的数据转换为树结构。因为我将使用EXTJS树面板,所以我需要将java对象存储为JSON格式:

Logic
---Propositional Logic
---Predicate Logic
---Modal Logic
-------Stit logic
-------Doxastic logic
---Temporal logic
我从excel工作表中读取数据,并使用multimap将每个键与多个值关联起来。 存储在multi-map中后的我的i/p:[Logic=>命题逻辑、谓词、模态、时态][modal Logic=stit、doxastic]

在stackoverflow论坛中搜索时,我发现一个示例代码如下: 我在这个函数中发送了multimap。我试着将其用于我的目标,并添加了gson代码以进行测试

public static Set <Tree> transform(Map <String, List<String>> input) {

    // Potential tree roots.  We start with all LHS keys as potential roots,
    // and eliminate them when we see their keys on the RHS.
    Set<String> roots = new HashSet<String>(input.keySet());

    // This map associates keys with the tree nodes that we create for them
    Map<String, Tree> map = new HashMap<String, Tree>();

   Gson gs = new Gson();
   String jsonString = null;

    for (Map.Entry<String, List<String>> entry : input.entrySet()) {
        String key = entry.getKey();
        List<String> childKeys = entry.getValue();
        Tree tree = map.get(key);
        if (tree == null) {
            tree = new Tree(key);
            map.put(key, tree);
        }
        for (String childKey : childKeys) {
           roots.remove(childKey);
            Tree child = map.get(childKey);
            if (child == null) {
                child = new Tree(childKey);
                map.put(childKey, child);
            }
            tree.addChild(child);
            jsonString =gs.toJson(tree);
        }


       System.out.println(jsonString);

    }

    Set<Tree> res = new HashSet<Tree>(roots.size());
    for (String key : roots) {
        res.add(map.get(key));
    }
   return res;
}
但我希望o/p是这样的:

{"key":"Logic","leaf":false,"children":[{"key":"Propositional Logic","leaf":true,"children":[]},{"key":"Predicate Logic","leaf":true,"children":[]},{"key":"Modal Logic","leaf":true,"children":[{"key":"STIT logic","leaf":true,"children":[]},{"key":"Doxastic Logic","leaf":true,"children":[]},{"key":"Coalition Logic","leaf":true,"children":[]}},{"key":"Temporal Logic","leaf":true,"children":[]}]}
我对Java编程不是很熟悉,所以我一直坚持这一点。以上代码中应添加哪些内容,请您提供建议

谢谢

{"key":"Logic","leaf":false,"children":[{"key":"Propositional Logic","leaf":true,"children":[]},{"key":"Predicate Logic","leaf":true,"children":[]},{"key":"Modal Logic","leaf":true,"children":[]},{"key":"Temporal Logic","leaf":true,"children":[]}]}

{"key":"Modal Logic","leaf":false,"children":[{"key":"STIT logic","leaf":true,"children":[]},{"key":"Doxastic Logic","leaf":true,"children":[]}]}
{"key":"Logic","leaf":false,"children":[{"key":"Propositional Logic","leaf":true,"children":[]},{"key":"Predicate Logic","leaf":true,"children":[]},{"key":"Modal Logic","leaf":true,"children":[{"key":"STIT logic","leaf":true,"children":[]},{"key":"Doxastic Logic","leaf":true,"children":[]},{"key":"Coalition Logic","leaf":true,"children":[]}},{"key":"Temporal Logic","leaf":true,"children":[]}]}