JAVAはデータベース表の内容に基づいて、ツリー構造JSONデータの実例コードを生産します。

18210 ワード

1、シーンを利用する
組織ツリーには、通常組織テーブルがあります。コード、コード(上位コード)、name(組織名)などのフィールドがあります。
2、データの構造(以下のデータは組織データではなく、本人がでっち上げたデータです。)

List> trees = new ArrayList>();
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 
* * @author kangxu2 2017-1-7 * */ public class Tree { /** * ID */ private String id; /** * */ private String text; /** * ,open closed */ private String state = "open"; /** * true false */ private boolean checked = false; /** * */ private List> attributes; /** * */ private List> children = new ArrayList>(); /** * 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> getAttributes() { return attributes; } public void setAttributes(List> attributes) { this.attributes = attributes; } public List> getChildren() { return children; } public void setChildren(List> 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> attributes, List> 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); } }
BuildTree.java

package pers.kangxu.datautils.common.tree;
import java.util.ArrayList;
import java.util.List;
import pers.kangxu.datautils.bean.tree.Tree;
/**
 *   tree
 * TODO
 * 
* @author kangxu2 2017-1-7 * */ public class BuildTree { /** * * TODO *
* @author kangxu2 2017-1-7 * * @param nodes * @return */ public static Tree build(List> nodes) { if(nodes == null){ return null; } List> topNodes = new ArrayList>(); for (Tree children : nodes) { String pid = children.getParentId(); if (pid == null || "".equals(pid)) { topNodes.add(children); continue; } for (Tree parent : nodes) { String id = parent.getId(); if (id != null && id.equals(pid)) { parent.getChildren().add(children); children.setParent(true); parent.setChildren(true); continue; } } } Tree root = new Tree(); 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; } }
BuildTreeTester.java

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> trees = new ArrayList>();
    List tests = new ArrayList();
    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 tree = new Tree();
      tree.setId(test.getId());
      tree.setParentId(test.getPid());
      tree.setText(test.getText());
      trees.add(tree);
    }
    Tree 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 + "]";
  }
}
4、運転結果
JSONデータ:

{
  "checked": true,
  "children": [
    {
      "checked": false,
      "children": [
        {
          "checked": false,
          "children": [
            {
              "checked": false,
              "children": [
                {
                  "checked": false,
                  "children": [],
                  "id": "12",
                  "parent": true,
                  "parentId": "3",
                  "state": "open",
                  "text": "String"
                },
                {
                  "checked": false,
                  "children": [],
                  "id": "18",
                  "parent": true,
                  "parentId": "3",
                  "state": "open",
                  "text": "  "
                }
              ],
              "id": "3",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "JAVA"
            },
            {
              "checked": false,
              "children": [
                {
                  "checked": false,
                  "children": [],
                  "id": "13",
                  "parent": true,
                  "parentId": "4",
                  "state": "open",
                  "text": "sql"
                },
                {
                  "checked": false,
                  "children": [],
                  "id": "19",
                  "parent": true,
                  "parentId": "4",
                  "state": "open",
                  "text": "  "
                }
              ],
              "id": "4",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "oracle"
            },
            {
              "checked": false,
              "children": [
                {
                  "checked": false,
                  "children": [],
                  "id": "14",
                  "parent": true,
                  "parentId": "5",
                  "state": "open",
                  "text": "ioc"
                },
                {
                  "checked": false,
                  "children": [],
                  "id": "15",
                  "parent": true,
                  "parentId": "5",
                  "state": "open",
                  "text": "aop"
                },
                {
                  "checked": false,
                  "children": [],
                  "id": "20",
                  "parent": true,
                  "parentId": "5",
                  "state": "open",
                  "text": "  "
                }
              ],
              "id": "5",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "spring"
            },
            {
              "checked": false,
              "children": [],
              "id": "6",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "springmvc"
            },
            {
              "checked": false,
              "children": [],
              "id": "7",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "fastdfs"
            },
            {
              "checked": false,
              "children": [],
              "id": "8",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "linux"
            },
            {
              "checked": false,
              "children": [],
              "id": "16",
              "parent": true,
              "parentId": "1",
              "state": "open",
              "text": "  "
            }
          ],
          "id": "1",
          "parent": true,
          "parentId": "0",
          "state": "open",
          "text": "    "
        },
        {
          "checked": false,
          "children": [
            {
              "checked": false,
              "children": [],
              "id": "9",
              "parent": true,
              "parentId": "2",
              "state": "open",
              "text": "  "
            },
            {
              "checked": false,
              "children": [],
              "id": "10",
              "parent": true,
              "parentId": "2",
              "state": "open",
              "text": "    "
            },
            {
              "checked": false,
              "children": [],
              "id": "11",
              "parent": true,
              "parentId": "2",
              "state": "open",
              "text": "  "
            },
            {
              "checked": false,
              "children": [],
              "id": "17",
              "parent": true,
              "parentId": "2",
              "state": "open",
              "text": "  "
            }
          ],
          "id": "2",
          "parent": true,
          "parentId": "0",
          "state": "open",
          "text": "  "
        }
      ],
      "id": "0",
      "parent": false,
      "parentId": "",
      "state": "open",
      "text": "    "
    }
  ],
  "id": "-1",
  "parent": false,
  "parentId": "",
  "state": "open",
  "text": "    "
}