Get the tree JSON string

Source: Internet
Author: User

public class TreeNode {

Private long nodeId;
Private String NodeName;
Private long Fathernodeid;

Public TreeNode () {
}
Public TreeNode (Long nodeId, String nodeName, long Fathernodeid) {
This.nodeid = nodeId;
This.nodename = NodeName;
This.fathernodeid = Fathernodeid;
}
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 Getfathernodeid () {
return Fathernodeid;
}
public void Setfathernodeid (long Fathernodeid) {
This.fathernodeid = Fathernodeid;
}

}


public class Constructortree {
StringBuffer json = new StringBuffer ();
/**
*
* @Title: Getjson
* @Description: Call interface
* @author Administrator
* @param list
* @param node
* @return
*/
Public String Getjson (list<treenode> List, TreeNode node) {
JSON = new StringBuffer ();
Constructorjson (list, node);
String jsondate = json.tostring ();
Return ("[" + Jsondate + "]"). ReplaceAll (",]", "]");


}

/**
*
* @Title: Constructorjson
* @Description: Building a JSON tree
* @author Administrator
* @param list
* @param treeNode
*/
@SuppressWarnings ("Unchecked")
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 ("},");
}
}

/**
*
* @Title: Getchildlist
* @Description: Get child node list information
* @author Administrator
* @param list
* @param node
* @return
*/
Public list<treenode> getchildlist (list<treenode> List, TreeNode node) {
list<treenode> li = new arraylist<treenode> ();
Iterator it = List.iterator ();
while (It.hasnext ()) {
TreeNode n = (TreeNode) it.next ();
if (n.getfathernodeid () = = Node.getnodeid ()) {
Li.add (n);
}
}
return Li;
}

/**
*
* @Title: Haschild
* @Description: Determine if there are child nodes
* @author Administrator
* @param list
* @param node
* @return
*/
public boolean haschild (list<treenode> List, TreeNode node) {
return getchildlist (list, node). Size () > 0? True:false;
}
}

Get the tree JSON string

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.