Converts data from a database with a parent-child relationship into JSON format using Java code

Source: Internet
Author: User

Ideas:
① all the parent nodes in the data into a collection
②, fetching all child nodes of the parent node into another collection in the data
③, using the iterative method, iterates through a layer of child nodes
Tool class:
Package com.assasion.test;

Import Net.sf.json.JSONArray;

Import java.util.ArrayList;
Import java.util.List;

public class Treebuilder {


list<companynode> nodes = new arraylist<companynode> ();

Public Treebuilder (list<companynode> nodes) {
Super ();
this.nodes= nodes;
}

/**
* Build JSON tree structure
* @return
*/
Public String Buildjsontree () {
list<companynode> Nodetree = Buildtree ();
Jsonarray Jsonarray = Jsonarray.fromobject (Nodetree);
return jsonarray.tostring ();
}

/**
* Build a tree-shaped structure
* @return
*/
Public list<companynode> Buildtree () {
list<companynode> treenodes = new arraylist<companynode> ();
list<companynode> rootnodes = Getrootnodes ();
for (Companynode rootnode:rootnodes) {
Buildchildnodes (RootNode);
Treenodes.add (RootNode);
}
return treenodes;
}

/**
* Recursive child nodes
* @param node
*/
public void Buildchildnodes (Companynode node) {
list<companynode> children = getchildnodes (node);
if (!children.isempty ()) {
for (Companynode Child:children) {
Buildchildnodes (child);
}
Node.setchild (children);
}
}

/**
* Gets all child nodes under the parent node
* @param pnode
* @return
*/
Public list<companynode> getchildnodes (Companynode pnode) {//Incoming parent node object, if the child node of the parent node is placed in the child node collection
list<companynode> childNodes = new arraylist<companynode> ();
for (Companynode n:nodes) {//filter from nodes so that the child nodes are Pnode
if (PNODE.GETCYBH (). Equals (N.GETSJ_CYBH ())) {
Childnodes.add (n);
}
}
return childNodes;
}

/**
* Determine if the root node
* @param node
* @return
*/
public boolean RootNode (Companynode node) {
Boolean isrootnode = true;
for (Companynode n:nodes) {//filter from nodes so the parent node
if (NODE.GETSJ_CYBH () Equals (N.GETCYBH ())) {//To determine the incoming node object, his superior member number also has no corresponding member number in node, and if not, the root node
Isrootnode= false;
Break
}
}
return isrootnode;
}

/**
* Gets all the root nodes in the collection
* @return
*/
Public list<companynode> Getrootnodes () {
list<companynode> rootnodes = new arraylist<companynode> ();
for (Companynode n:nodes) {
if (RootNode (n)) {
Rootnodes.add (n);//Put the root node into the Rootnodes collection
}
}
return rootnodes;
}
}
Entity class:
Package com.assasion.test;

Import java.util.List;

public class Companynode {
private String name;//Company Name
Private String cybh;//Member number
Private String sj_cybh;//Superior member number
Private list<companynode> child;//Subordinate company

Public Companynode () {
}

Public Companynode (string name, String cybh, String sj_cybh,list<companynode> child) {
THIS.name = name;
THIS.CYBH = CYBH;
THIS.SJ_CYBH = SJ_CYBH;
This.child=child;
}

Public list<companynode> Getchild () {
return child;
}

public void SetChild (List<companynode> child) {
This.child = child;
}

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

Public String getcybh () {
return CYBH;
}

public void Setcybh (String cybh) {
THIS.CYBH = CYBH;
}

Public String getsj_cybh () {
return SJ_CYBH;
}

public void Setsj_cybh (String sj_cybh) {
THIS.SJ_CYBH = SJ_CYBH;
}
}

Converts data from a database with a parent-child relationship into JSON format using Java code

Related Article

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.