开发者

How to achieve a json tree structure format

开发者 https://www.devze.com 2023-04-05 01:22 出处:网络
My requirement to convert data from excel into tree structure. As I will be using EXTJS tree panel so i need to store java object into JSON format:

My requirement to convert data from excel into tree structure. As I will be using EXTJS tree panel so i need to store java object into JSON format:

Logic
---Propositional Logic
---Predicate Logic
---Modal Logic
-------Stit logic
-------Doxastic logic
---Temporal logic

I read data from excel sheet and used multimap in order to associate each key with multiple values. My i/p after storing in multi map: [Logic=>Propositional logic, predicate,modal,temporal] [modal logic= stit, doxastic]

While searching in stackoverflow forum, i found a sample code which is like this: I sent multimap in this function. I tried to use it for my objective and added gson codes in order to test.

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

I also have a Tree class :

public class Tree{

    private String  key;
    private boolean leaf;
    private List<Tree> children = new ArrayList<Tree>();

    public Tree(String key)
    {
        this.key=key;
        leaf=true;
    }

    public void addChild(Tree child)
    {
        children.add(child);
        leaf=false;
    }

}

The o/p i get is :

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

But i wanted the o/p to be like:

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

I am not very much familiar with Java programming so i am stuck with this. What should be added in above codes, can you please advice?

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消