I want to create a way to convert a JSONObject like this:
"master root" : {
"folder 1" : {
"folder 2" : {
"example data" : "some more example values",
"example data" : "some more examples"
},
"example data" : "example value"
},
"example data" : "lol"
}
to something like this:
I have tried a method that looks something like this:
public javax.swing.tree.DefaultMutableTreeNode createSingleTreeNode(JSONObject obj) {
for(Iterator iterator = obj.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
if (obj.get(key) instanceof JSONObject) {
javax.swing.tree.DefaultMutableTreeNode node1 = new javax.swing.tree.DefaultMutableTreeNode(key);
node1.add(createSingleTreeNode((JSONObject) obj.get(key)));
return node1;
} else {
return new javax.swing.tree.DefaultMutableTreeNode(key);
}
}
return new javax.swing.tree.DefaultMutableTreeNode("");
}
It works to some degree but stops after it gets to the first item that isn't a json object it just stops and only adds one.
Any ideas on how to do this properly? (I'm new to java so i'm sorry if there is a library that does this already)
I fixed it, instead of returning the node, it will add the node to the "master" node that you add as a parameter, here is the code