I have a JSON tree structure that is generated dinamically, this means the keys and the structure are unknown and it can change in every request, the only common property in every object are the oldValue and newValue keys...
the JSON looks like this:
{
"obj1":
{"oldValue": "foo", "newValue": "foofoo"},
"obj2":
{"obj2_1":
{"oldValue": null, "newValue": "foo"}
},
{"obj2_2":
{"obj2_2_1":
{"oldValue": "foo", "newValue": "foofoo"}
},
{"obj2_2_2":
{"oldValue": "foo1", "newValue": null}
}
}
}
How can I load this data in a TreeGrid dinamically? Is it possible to get to the deepest key using XPath?
I came to the conclusion that is just not possible with SmartGWT because of the fact that, in SmartGWT components that can handle hierarchical data, you need to specify a parent key as an attribute, wich is not very practical (IMHO)...
Now the solution: The best alternative to this is to use the GWT core widgets Tree and TreeItem, with TreeItem you can attach items dinamically, so I loaded the json data into a JSONObject and added the nodes recursively.
Below the tree nodes loading process:
private void loadTreeNodes(JSONObject jsonObject, TreeItem parent) {
for (String key: jsonObject.keySet()) {
JSONValue jsonValue = jsonObject.get(key);
TreeItem item = new TreeItem("<b>" + key + "</b> ");
parent.addItem(item);
if (jsonValue != null) {
if (jsonValue.isObject() != null) {
loadTreeNodes(jsonValue.isObject(), item);
} else if (jsonValue.isArray() != null) {
} else {
if ("oldValue".equals(key)) {
item.setHTML("<b>before:</b> " + jsonValue.toString());
} else {
item.setHTML("<b>after:</b> " + jsonValue.toString());
}
}
} else {
item.setText("<b>" + item.getText() + ":</b> null");
}
}
}
I loaded the json data like this:
JavaScriptObject obj = JSON.decode(jsonData);
JSONObject jsonObject = new JSONObject(obj);
Any comments are welcome!