Building the JSON Tree directory structure

Source: Internet
Author: User
Tags stringbuffer

The program is mainly assembled in JSON-formatted tree data files, divided into two parts:

A simple pojo that represents a node

public class TreeNode {

Private long nodeId;

Private String nodename;

Private long Father_nodeid;

Public TreeNode () {}

Public TreeNode (Long nodeid,string nodename,long Father_nodeid) {
This.nodeid=nodeid;
This.nodename=nodename;
This.father_nodeid=father_nodeid;
}

Public long Getnodeid () {
return nodeId;
}

public void Setnodeid (long nodeId) {
This.nodeid = nodeId;
}

Public String Getnodename () {
return nodename;
}

public void Setnodename (String nodename) {
This.nodename = nodename;
}

Public long Getfather_nodeid () {
return Father_nodeid;
}

public void Setfather_nodeid (long Fathernodeid) {
Father_nodeid = Fathernodeid;
}
}

The tool class of the two building trees

public class Constructortree {

StringBuffer json=new StringBuffer ();

/**
* Build JSON file
* @param list
* @param node
*/
public void Constructorjson (list<treenode> List, TreeNode TreeNode) {
if (Haschild (List,treenode)) {
Json.append ("{/" Id/":/");
Json.append (Treenode.getnodeid () + "/");
Json.append (",/" text/":/");
Json.append (Treenode.getnodename () + "/");
Json.append (",/" children/": [");
list<treenode> childlist = getchildlist (List,treenode);
Iterator iterator = Childlist.iterator ();
while (Iterator.hasnext ()) {
TreeNode node = (TreeNode) iterator.next ();
Constructorjson (List,node);
}
Json.append ("]},");
}else{
Json.append ("{/" Id/":/");
Json.append (Treenode.getnodeid () + "/");
Json.append (",/" text/":/");
Json.append (Treenode.getnodename () + "/");
Json.append ("},");
}

}

/**
* Get sub-node list information
* @param list
* @param node
* @return
*/
Public list<treenode> getchildlist (list<treenode> list, TreeNode node) {//Get child node list
list<treenode> li = new arraylist<treenode> ();
Iterator it = List.iterator ();
while (It.hasnext ()) {
TreeNode n = (TreeNode) it.next ();
if (N.getfather_nodeid () ==node.getnodeid ()) {
Li.add (n);
}
}
return Li;
}

/**
* Determine if there are child nodes
* @param list
* @param node
* @return
*/
public boolean haschild (list<treenode> List, TreeNode node) {
Return Getchildlist (List,node). Size () >0?true:false;
}
Public String Getjson (list<treenode> List, TreeNode node) {
JSON = new StringBuffer ();
Constructorjson (List,node);
String jsondate=json.tostring ();
Return ("[" +jsondate+ "]"). ReplaceAll (",]", "]");

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.