Following code will convert a tree into JSON data, without using JSONObject.
More specifically it will output the required JSON data for a Zapatec tree !
private Stack stack = new Stack();
public StringBuffer toJSONString(GenericNode currentNode){
if (currentNode != null) {
sb.append("{ label: \"").append(currentNode.getCurrentNode().getBudgetDescription()).append("\",");
stack.push("}");
sb.append("attributes: {").append("id: \"").append(currentNode.getCurrentNode().getBudgetId()).append("\"}");
}
if (currentNode != null && currentNode.hasChildren()){
sb.append(", children : [ ");
stack.push("]}");
for (GenericNode node : currentNode.getChildren()) {
toJSONString(node);
}
if (stack.size()>0) {
sb.deleteCharAt(sb.length()-1);
sb.append(stack.pop());
}
} else sb.append("},");
return sb;
}
Thursday, July 31, 2008
Tree to JSON Conversion
Labels:
JSON,
JSONObject,
Recursive,
Tree,
Zapatec
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment