在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

JAVA 根据数据库表内容生产树结构JSON数据的实例代码

2017-3-7 12:50| 发布者: zhangjf| 查看: 1388| 评论: 0

摘要: 1、利用场景   组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段 2、构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据) ListTreeTest trees = new Array ...

1、利用场景

  组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段

2、构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据)

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

3、源码

Tree.java

  1. package pers.kangxu.datautils.bean.tree;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import com.alibaba.fastjson.JSON;
  6. /**
  7. * tree TODO <br>
  8. *
  9. * @author kangxu2 2017-1-7
  10. *
  11. */
  12. public class Tree<T> {
  13. /**
  14. * 节点ID
  15. */
  16. private String id;
  17. /**
  18. * 显示节点文本
  19. */
  20. private String text;
  21. /**
  22. * 节点状态,open closed
  23. */
  24. private String state = "open";
  25. /**
  26. * 节点是否被选中 true false
  27. */
  28. private boolean checked = false;
  29. /**
  30. * 节点属性
  31. */
  32. private List<Map<String, Object>> attributes;
  33. /**
  34. * 节点的子节点
  35. */
  36. private List<Tree<T>> children = new ArrayList<Tree<T>>();
  37. /**
  38. * 父ID
  39. */
  40. private String parentId;
  41. /**
  42. * 是否有父节点
  43. */
  44. private boolean isParent = false;
  45. /**
  46. * 是否有子节点
  47. */
  48. private boolean isChildren = false;
  49. public String getId() {
  50. return id;
  51. }
  52. public void setId(String id) {
  53. this.id = id;
  54. }
  55. public String getText() {
  56. return text;
  57. }
  58. public void setText(String text) {
  59. this.text = text;
  60. }
  61. public String getState() {
  62. return state;
  63. }
  64. public void setState(String state) {
  65. this.state = state;
  66. }
  67. public boolean isChecked() {
  68. return checked;
  69. }
  70. public void setChecked(boolean checked) {
  71. this.checked = checked;
  72. }
  73. public List<Map<String, Object>> getAttributes() {
  74. return attributes;
  75. }
  76. public void setAttributes(List<Map<String, Object>> attributes) {
  77. this.attributes = attributes;
  78. }
  79. public List<Tree<T>> getChildren() {
  80. return children;
  81. }
  82. public void setChildren(List<Tree<T>> children) {
  83. this.children = children;
  84. }
  85. public boolean isParent() {
  86. return isParent;
  87. }
  88. public void setParent(boolean isParent) {
  89. this.isParent = isParent;
  90. }
  91. public boolean isChildren() {
  92. return isChildren;
  93. }
  94. public void setChildren(boolean isChildren) {
  95. this.isChildren = isChildren;
  96. }
  97. public String getParentId() {
  98. return parentId;
  99. }
  100. public void setParentId(String parentId) {
  101. this.parentId = parentId;
  102. }
  103. public Tree(String id, String text, String state, boolean checked,
  104. List<Map<String, Object>> attributes, List<Tree<T>> children,
  105. boolean isParent, boolean isChildren, String parentID) {
  106. super();
  107. this.id = id;
  108. this.text = text;
  109. this.state = state;
  110. this.checked = checked;
  111. this.attributes = attributes;
  112. this.children = children;
  113. this.isParent = isParent;
  114. this.isChildren = isChildren;
  115. this.parentId = parentID;
  116. }
  117. public Tree() {
  118. super();
  119. }
  120. @Override
  121. public String toString() {
  122. return JSON.toJSONString(this);
  123. }
  124. }
复制代码

BuildTree.java

  1. package pers.kangxu.datautils.common.tree;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import pers.kangxu.datautils.bean.tree.Tree;
  5. /**
  6. * 构建tree
  7. * TODO
  8. * <br>
  9. * @author kangxu2 2017-1-7
  10. *
  11. */
  12. public class BuildTree {
  13. /**
  14. *
  15. * TODO
  16. * <br>
  17. * @author kangxu2 2017-1-7
  18. *
  19. * @param nodes
  20. * @return
  21. */
  22. public static <T> Tree<T> build(List<Tree<T>> nodes) {
  23. if(nodes == null){
  24. return null;
  25. }
  26. List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
  27. for (Tree<T> children : nodes) {
  28. String pid = children.getParentId();
  29. if (pid == null || "".equals(pid)) {
  30. topNodes.add(children);
  31. continue;
  32. }
  33. for (Tree<T> parent : nodes) {
  34. String id = parent.getId();
  35. if (id != null && id.equals(pid)) {
  36. parent.getChildren().add(children);
  37. children.setParent(true);
  38. parent.setChildren(true);
  39. continue;
  40. }
  41. }
  42. }
  43. Tree<T> root = new Tree<T>();
  44. if (topNodes.size() == 0) {
  45. root = topNodes.get(0);
  46. } else {
  47. root.setId("-1");
  48. root.setParentId("");
  49. root.setParent(false);
  50. root.setChildren(true);
  51. root.setChecked(true);
  52. root.setChildren(topNodes);
  53. root.setText("顶级节点");
  54. }
  55. return root;
  56. }
  57. }
复制代码

BuildTreeTester.java

  1. package pers.kangxu.datautils.test;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import pers.kangxu.datautils.bean.tree.Tree;
  5. import pers.kangxu.datautils.common.tree.BuildTree;
  6. public class BuildTreeTester {
  7. public static void main(String[] args) {
  8. List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
  9. List<Test> tests = new ArrayList<Test>();
  10. tests.add(new Test("0", "", "关于本人"));
  11. tests.add(new Test("1", "0", "技术学习"));
  12. tests.add(new Test("2", "0", "兴趣"));
  13. tests.add(new Test("3", "1", "JAVA"));
  14. tests.add(new Test("4", "1", "oracle"));
  15. tests.add(new Test("5", "1", "spring"));
  16. tests.add(new Test("6", "1", "springmvc"));
  17. tests.add(new Test("7", "1", "fastdfs"));
  18. tests.add(new Test("8", "1", "linux"));
  19. tests.add(new Test("9", "2", "骑行"));
  20. tests.add(new Test("10", "2", "吃喝玩乐"));
  21. tests.add(new Test("11", "2", "学习"));
  22. tests.add(new Test("12", "3", "String"));
  23. tests.add(new Test("13", "4", "sql"));
  24. tests.add(new Test("14", "5", "ioc"));
  25. tests.add(new Test("15", "5", "aop"));
  26. tests.add(new Test("16", "1", "等等"));
  27. tests.add(new Test("17", "2", "等等"));
  28. tests.add(new Test("18", "3", "等等"));
  29. tests.add(new Test("19", "4", "等等"));
  30. tests.add(new Test("20", "5", "等等"));
  31. for (Test test : tests) {
  32. Tree<Test> tree = new Tree<Test>();
  33. tree.setId(test.getId());
  34. tree.setParentId(test.getPid());
  35. tree.setText(test.getText());
  36. trees.add(tree);
  37. }
  38. Tree<Test> t = BuildTree.build(trees);
  39. System.out.println(t);
  40. }
  41. }
  42. class Test {
  43. private String id;
  44. private String pid;
  45. private String text;
  46. public String getId() {
  47. return id;
  48. }
  49. public void setId(String id) {
  50. this.id = id;
  51. }
  52. public String getPid() {
  53. return pid;
  54. }
  55. public void setPid(String pid) {
  56. this.pid = pid;
  57. }
  58. public String getText() {
  59. return text;
  60. }
  61. public void setText(String text) {
  62. this.text = text;
  63. }
  64. public Test(String id, String pid, String text) {
  65. super();
  66. this.id = id;
  67. this.pid = pid;
  68. this.text = text;
  69. }
  70. public Test() {
  71. super();
  72. }
  73. @Override
  74. public String toString() {
  75. return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";
  76. }
  77. }
复制代码

4、运行结果

JSON数据:

  1. {
  2. "checked": true,
  3. "children": [
  4. {
  5. "checked": false,
  6. "children": [
  7. {
  8. "checked": false,
  9. "children": [
  10. {
  11. "checked": false,
  12. "children": [
  13. {
  14. "checked": false,
  15. "children": [],
  16. "id": "12",
  17. "parent": true,
  18. "parentId": "3",
  19. "state": "open",
  20. "text": "String"
  21. },
  22. {
  23. "checked": false,
  24. "children": [],
  25. "id": "18",
  26. "parent": true,
  27. "parentId": "3",
  28. "state": "open",
  29. "text": "等等"
  30. }
  31. ],
  32. "id": "3",
  33. "parent": true,
  34. "parentId": "1",
  35. "state": "open",
  36. "text": "JAVA"
  37. },
  38. {
  39. "checked": false,
  40. "children": [
  41. {
  42. "checked": false,
  43. "children": [],
  44. "id": "13",
  45. "parent": true,
  46. "parentId": "4",
  47. "state": "open",
  48. "text": "sql"
  49. },
  50. {
  51. "checked": false,
  52. "children": [],
  53. "id": "19",
  54. "parent": true,
  55. "parentId": "4",
  56. "state": "open",
  57. "text": "等等"
  58. }
  59. ],
  60. "id": "4",
  61. "parent": true,
  62. "parentId": "1",
  63. "state": "open",
  64. "text": "oracle"
  65. },
  66. {
  67. "checked": false,
  68. "children": [
  69. {
  70. "checked": false,
  71. "children": [],
  72. "id": "14",
  73. "parent": true,
  74. "parentId": "5",
  75. "state": "open",
  76. "text": "ioc"
  77. },
  78. {
  79. "checked": false,
  80. "children": [],
  81. "id": "15",
  82. "parent": true,
  83. "parentId": "5",
  84. "state": "open",
  85. "text": "aop"
  86. },
  87. {
  88. "checked": false,
  89. "children": [],
  90. "id": "20",
  91. "parent": true,
  92. "parentId": "5",
  93. "state": "open",
  94. "text": "等等"
  95. }
  96. ],
  97. "id": "5",
  98. "parent": true,
  99. "parentId": "1",
  100. "state": "open",
  101. "text": "spring"
  102. },
  103. {
  104. "checked": false,
  105. "children": [],
  106. "id": "6",
  107. "parent": true,
  108. "parentId": "1",
  109. "state": "open",
  110. "text": "springmvc"
  111. },
  112. {
  113. "checked": false,
  114. "children": [],
  115. "id": "7",
  116. "parent": true,
  117. "parentId": "1",
  118. "state": "open",
  119. "text": "fastdfs"
  120. },
  121. {
  122. "checked": false,
  123. "children": [],
  124. "id": "8",
  125. "parent": true,
  126. "parentId": "1",
  127. "state": "open",
  128. "text": "linux"
  129. },
  130. {
  131. "checked": false,
  132. "children": [],
  133. "id": "16",
  134. "parent": true,
  135. "parentId": "1",
  136. "state": "open",
  137. "text": "等等"
  138. }
  139. ],
  140. "id": "1",
  141. "parent": true,
  142. "parentId": "0",
  143. "state": "open",
  144. "text": "技术学习"
  145. },
  146. {
  147. "checked": false,
  148. "children": [
  149. {
  150. "checked": false,
  151. "children": [],
  152. "id": "9",
  153. "parent": true,
  154. "parentId": "2",
  155. "state": "open",
  156. "text": "骑行"
  157. },
  158. {
  159. "checked": false,
  160. "children": [],
  161. "id": "10",
  162. "parent": true,
  163. "parentId": "2",
  164. "state": "open",
  165. "text": "吃喝玩乐"
  166. },
  167. {
  168. "checked": false,
  169. "children": [],
  170. "id": "11",
  171. "parent": true,
  172. "parentId": "2",
  173. "state": "open",
  174. "text": "学习"
  175. },
  176. {
  177. "checked": false,
  178. "children": [],
  179. "id": "17",
  180. "parent": true,
  181. "parentId": "2",
  182. "state": "open",
  183. "text": "等等"
  184. }
  185. ],
  186. "id": "2",
  187. "parent": true,
  188. "parentId": "0",
  189. "state": "open",
  190. "text": "兴趣"
  191. }
  192. ],
  193. "id": "0",
  194. "parent": false,
  195. "parentId": "",
  196. "state": "open",
  197. "text": "关于本人"
  198. }
  199. ],
  200. "id": "-1",
  201. "parent": false,
  202. "parentId": "",
  203. "state": "open",
  204. "text": "顶级节点"
  205. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 01:39

Copyright 2015-2025 djqfx

返回顶部