首頁 > Java > java教程 > JAVA 根據資料庫表格內容生產樹狀結構JSON資料的實例代碼

JAVA 根據資料庫表格內容生產樹狀結構JSON資料的實例代碼

高洛峰
發布: 2017-01-10 12:49:33
原創
1313 人瀏覽過

1、利用情境

  組織機構樹,通常會有組織機構表,其中有code(代碼),pcode(上級代碼),name(組織名稱)等欄位

2、建構資料(以下資料並非組織機構數據,而純屬本人胡編亂造的數據)

List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
tests.add(new Test("0", "", "关于本人"));
tests.add(new Test("1", "0", "技术学习"));
tests.add(new Test("2", "0", "兴趣"));
tests.add(new Test("3", "1", "JAVA"));
tests.add(new Test("4", "1", "oracle"));
tests.add(new Test("5", "1", "spring"));
tests.add(new Test("6", "1", "springmvc"));
tests.add(new Test("7", "1", "fastdfs"));
tests.add(new Test("8", "1", "linux"));
tests.add(new Test("9", "2", "骑行"));
tests.add(new Test("10", "2", "吃喝玩乐"));
tests.add(new Test("11", "2", "学习"));
tests.add(new Test("12", "3", "String"));
tests.add(new Test("13", "4", "sql"));
tests.add(new Test("14", "5", "ioc"));
tests.add(new Test("15", "5", "aop"));
tests.add(new Test("16", "1", "等等"));
tests.add(new Test("17", "2", "等等"));
tests.add(new Test("18", "3", "等等"));
tests.add(new Test("19", "4", "等等"));
tests.add(new Test("20", "5", "等等"));
登入後複製

   

3、源碼

Tree.java

package pers.kangxu.datautils.bean.tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
/**
 * tree TODO <br>
 *
 * @author kangxu2 2017-1-7
 *
 */
public class Tree<T> {
  /**
   * 节点ID
   */
  private String id;
  /**
   * 显示节点文本
   */
  private String text;
  /**
   * 节点状态,open closed
   */
  private String state = "open";
  /**
   * 节点是否被选中 true false
   */
  private boolean checked = false;
  /**
   * 节点属性
   */
  private List<Map<String, Object>> attributes;
  /**
   * 节点的子节点
   */
  private List<Tree<T>> children = new ArrayList<Tree<T>>();
  /**
   * 父ID
   */
  private String parentId;
  /**
   * 是否有父节点
   */
  private boolean isParent = false;
  /**
   * 是否有子节点
   */
  private boolean isChildren = false;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public String getState() {
    return state;
  }
  public void setState(String state) {
    this.state = state;
  }
  public boolean isChecked() {
    return checked;
  }
  public void setChecked(boolean checked) {
    this.checked = checked;
  }
  public List<Map<String, Object>> getAttributes() {
    return attributes;
  }
  public void setAttributes(List<Map<String, Object>> attributes) {
    this.attributes = attributes;
  }
  public List<Tree<T>> getChildren() {
    return children;
  }
  public void setChildren(List<Tree<T>> children) {
    this.children = children;
  }
  public boolean isParent() {
    return isParent;
  }
  public void setParent(boolean isParent) {
    this.isParent = isParent;
  }
  public boolean isChildren() {
    return isChildren;
  }
  public void setChildren(boolean isChildren) {
    this.isChildren = isChildren;
  }
  public String getParentId() {
    return parentId;
  }
  public void setParentId(String parentId) {
    this.parentId = parentId;
  }
  public Tree(String id, String text, String state, boolean checked,
      List<Map<String, Object>> attributes, List<Tree<T>> children,
      boolean isParent, boolean isChildren, String parentID) {
    super();
    this.id = id;
    this.text = text;
    this.state = state;
    this.checked = checked;
    this.attributes = attributes;
    this.children = children;
    this.isParent = isParent;
    this.isChildren = isChildren;
    this.parentId = parentID;
  }
  public Tree() {
    super();
  }
  @Override
  public String toString() {
    return JSON.toJSONString(this);
  }
}
登入後複製

BuildTreeTester.java

package pers.kangxu.datautils.common.tree;
import java.util.ArrayList;
import java.util.List;
import pers.kangxu.datautils.bean.tree.Tree;
/**
 * 构建tree
 * TODO
 * <br>
 * @author kangxu2 2017-1-7
 *
 */
public class BuildTree {
  /**
   *
   * TODO
   * <br>
   * @author kangxu2 2017-1-7
   *
   * @param nodes
   * @return
   */
  public static <T> Tree<T> build(List<Tree<T>> nodes) {
    if(nodes == null){
      return null;
    }
    List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
    for (Tree<T> children : nodes) {
      String pid = children.getParentId();
      if (pid == null || "".equals(pid)) {
        topNodes.add(children);
        continue;
      }
      for (Tree<T> parent : nodes) {
        String id = parent.getId();
        if (id != null && id.equals(pid)) {
          parent.getChildren().add(children);
          children.setParent(true);
          parent.setChildren(true);
          continue;
        }
      }
    }
    Tree<T> root = new Tree<T>();
    if (topNodes.size() == 0) {
      root = topNodes.get(0);
    } else {
      root.setId("-1");
      root.setParentId("");
      root.setParent(false);
      root.setChildren(true);
      root.setChecked(true);
      root.setChildren(topNodes);
      root.setText("顶级节点");
    }
    return root;
  }
}
登入後複製

   

4、運行結果

JSON資料:

package pers.kangxu.datautils.test;
import java.util.ArrayList;
import java.util.List;
import pers.kangxu.datautils.bean.tree.Tree;
import pers.kangxu.datautils.common.tree.BuildTree;
public class BuildTreeTester {
  public static void main(String[] args) {
    List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
    List<Test> tests = new ArrayList<Test>();
    tests.add(new Test("0", "", "关于本人"));
    tests.add(new Test("1", "0", "技术学习"));
    tests.add(new Test("2", "0", "兴趣"));
    tests.add(new Test("3", "1", "JAVA"));
    tests.add(new Test("4", "1", "oracle"));
    tests.add(new Test("5", "1", "spring"));
    tests.add(new Test("6", "1", "springmvc"));
    tests.add(new Test("7", "1", "fastdfs"));
    tests.add(new Test("8", "1", "linux"));
    tests.add(new Test("9", "2", "骑行"));
    tests.add(new Test("10", "2", "吃喝玩乐"));
    tests.add(new Test("11", "2", "学习"));
    tests.add(new Test("12", "3", "String"));
    tests.add(new Test("13", "4", "sql"));
    tests.add(new Test("14", "5", "ioc"));
    tests.add(new Test("15", "5", "aop"));
    tests.add(new Test("16", "1", "等等"));
    tests.add(new Test("17", "2", "等等"));
    tests.add(new Test("18", "3", "等等"));
    tests.add(new Test("19", "4", "等等"));
    tests.add(new Test("20", "5", "等等"));
    for (Test test : tests) {
      Tree<Test> tree = new Tree<Test>();
      tree.setId(test.getId());
      tree.setParentId(test.getPid());
      tree.setText(test.getText());
      trees.add(tree);
    }
    Tree<Test> t = BuildTree.build(trees);
    System.out.println(t);
  }
}
class Test {
  private String id;
  private String pid;
  private String text;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getPid() {
    return pid;
  }
  public void setPid(String pid) {
    this.pid = pid;
  }
  public String getText() {
    return text;
  }
  public void setText(String text) {
    this.text = text;
  }
  public Test(String id, String pid, String text) {
    super();
    this.id = id;
    this.pid = pid;
    this.text = text;
  }
  public Test() {
    super();
  }
  @Override
  public String toString() {
    return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";
  }
}
登入後複製

更多JAVA 根據資料庫表內容生產樹結構JSON資料的實例代碼相關文章請關注PHPPHP!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板