Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to achieve a json tree structure format
    text
    copied!<p>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:</p> <pre><code>Logic ---Propositional Logic ---Predicate Logic ---Modal Logic -------Stit logic -------Doxastic logic ---Temporal logic </code></pre> <p>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]</p> <p>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.</p> <pre><code>public static Set &lt;Tree&gt; transform(Map &lt;String, List&lt;String&gt;&gt; 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&lt;String&gt; roots = new HashSet&lt;String&gt;(input.keySet()); // This map associates keys with the tree nodes that we create for them Map&lt;String, Tree&gt; map = new HashMap&lt;String, Tree&gt;(); Gson gs = new Gson(); String jsonString = null; for (Map.Entry&lt;String, List&lt;String&gt;&gt; entry : input.entrySet()) { String key = entry.getKey(); List&lt;String&gt; 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&lt;Tree&gt; res = new HashSet&lt;Tree&gt;(roots.size()); for (String key : roots) { res.add(map.get(key)); } return res; } </code></pre> <p>I also have a Tree class :</p> <pre><code>public class Tree{ private String key; private boolean leaf; private List&lt;Tree&gt; children = new ArrayList&lt;Tree&gt;(); public Tree(String key) { this.key=key; leaf=true; } public void addChild(Tree child) { children.add(child); leaf=false; } } </code></pre> <p>The o/p i get is :</p> <pre><code>{"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":[]}]} </code></pre> <p>But i wanted the o/p to be like:</p> <pre><code>{"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":[]}]} </code></pre> <p>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?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload