JSON

JAVA 根据数据库表内容生产树结构JSON数据(2)

字号+ 作者:H5之家 来源:H5之家 2017-01-10 08:07 我要评论( )

BuildTree.java 1 package pers.kangxu.datautils.common.tree; java.util.ArrayList; 4 import java.util.List; pers.kangxu.datautils.bean.tree.Tree; * 构建tree 10 * TODO 11 * br kangxu2 2017-1-7 13 * Buil

BuildTree.java

1 package pers.kangxu.datautils.common.tree; java.util.ArrayList; 4 import java.util.List; pers.kangxu.datautils.bean.tree.Tree; * 构建tree 10 * TODO 11 * <br> kangxu2 2017-1-7 13 * BuildTree { * 19 * TODO 20 * <br> kangxu2 2017-1-7 22 * nodes <T> Tree<T> build(List<Tree<T>> nodes) { (nodes == null){ ; 30 } 31 List<Tree<T>> topNodes = new ArrayList<Tree<T>>(); (Tree<T> children : nodes) { 34 35 String pid = children.getParentId(); 36 if (pid == null || "".equals(pid)) { 37 topNodes.add(children); ; 40 } (Tree<T> parent : nodes) { 43 String id = parent.getId(); 44 if (id != null && id.equals(pid)) { 45 parent.getChildren().add(children); 46 children.setParent(true); 47 parent.setChildren(true); ; 50 } 51 } 52 53 } 54 55 Tree<T> root = new Tree<T>(); 56 if (topNodes.size() == 0) { 57 root = topNodes.get(0); 58 } else { 59 root.setId("-1"); 60 root.setParentId(""); 61 root.setParent(false); 62 root.setChildren(true); 63 root.setChecked(true); 64 root.setChildren(topNodes); 65 root.setText("顶级节点"); 66 67 } root; 70 } 71 72 }

 

BuildTreeTester.java

1 package pers.kangxu.datautils.test; java.util.ArrayList; 4 import java.util.List; pers.kangxu.datautils.bean.tree.Tree; 7 import pers.kangxu.datautils.common.tree.BuildTree; BuildTreeTester { main(String[] args) { List<Tree<Test>> trees = new ArrayList<Tree<Test>>(); 15 List<Test> tests = new ArrayList<Test>(); 16 tests.add(new Test("0", "", "关于本人")); 17 tests.add(new Test("1", "0", "技术学习")); 18 tests.add(new Test("2", "0", "兴趣")); 19 tests.add(new Test("3", "1", "JAVA")); 20 tests.add(new Test("4", "1", "oracle")); 21 tests.add(new Test("5", "1", "spring")); 22 tests.add(new Test("6", "1", "springmvc")); 23 tests.add(new Test("7", "1", "fastdfs")); 24 tests.add(new Test("8", "1", "linux")); 25 tests.add(new Test("9", "2", "骑行")); 26 tests.add(new Test("10", "2", "吃喝玩乐")); 27 tests.add(new Test("11", "2", "学习")); 28 tests.add(new Test("12", "3", "String")); 29 tests.add(new Test("13", "4", "sql")); 30 tests.add(new Test("14", "5", "ioc")); 31 tests.add(new Test("15", "5", "aop")); 32 tests.add(new Test("16", "1", "等等")); 33 tests.add(new Test("17", "2", "等等")); 34 tests.add(new Test("18", "3", "等等")); 35 tests.add(new Test("19", "4", "等等")); 36 tests.add(new Test("20", "5", "等等")); (Test test : tests) { 39 Tree<Test> tree = new Tree<Test>(); 40 tree.setId(test.getId()); 41 tree.setParentId(test.getPid()); 42 tree.setText(test.getText()); 43 44 trees.add(tree); 45 } 46 47 Tree<Test> t = BuildTree.build(trees); 48 System.out.println(t); 49 } 50 } Test { String id; 55 private String pid; 56 private String text; String getId() { 59 return id; 60 } setId(String id) { 63 this.id = id; 64 } String getPid() { 67 return pid; 68 } setPid(String pid) { 71 this.pid = pid; 72 } String getText() { 75 return text; 76 } setText(String text) { 79 this.text = text; 80 } Test(String id, String pid, String text) { 83 super(); 84 this.id = id; 85 this.pid = pid; 86 this.text = text; 87 } Test() { 90 super(); 91 } 92 93 @Override 94 public String toString() { 95 return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]"; 96 } 97 98 }

4、运行结果

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Retrofit+RxJava,Http请求get和post方法获取json数据简单封装

    Retrofit+RxJava,Http请求get和post方法获取json数据简单封装

    2017-01-06 13:00

  • JAVA解析JSON数据代码

    JAVA解析JSON数据代码

    2017-01-06 09:02

  • JS将JSON对象转化为字符串的改进方法

    JS将JSON对象转化为字符串的改进方法

    2017-01-02 13:00

  • 在application/json,application/javascript等Response下进行XSS

    在application/json,application/javascript等Response下进行XSS

    2016-12-30 18:01

网友点评
<