ExtJs树的递归算法(Java),Json格式
来源:互联网 发布:淘宝小帮手 编辑:程序博客网 时间:2024/06/06 00:43
首先先建立Node模型
public class Node { private int id; private int parentId; Node(){} Node(int id,int parentId){ this.id=id; this.parentId = parentId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } }
下面这个类先手工建立List,在实际应用中是从数据库读取List,然后再Main方法里调用递归方法,得到Json字符串
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Recursion { List nodeList =new ArrayList(); Recursion(){//构造方法里初始化模拟List Node node1 = new Node(1,0); Node node2 = new Node(2,1); Node node3 = new Node(3,1); Node node4 = new Node(4,2); Node node5 = new Node(5,2); Node node6 = new Node(6,2); Node node7 = new Node(7,6); Node node8 = new Node(8,6); nodeList.add(node1); nodeList.add(node2); nodeList.add(node3); nodeList.add(node4); nodeList.add(node5); nodeList.add(node6); nodeList.add(node7); nodeList.add(node8); } StringBuffer returnStr=new StringBuffer(); public void recursionFn(List list , Node node){ if(hasChild(list,node)){ returnStr.append("{id:"); returnStr.append(node.getId()); returnStr.append(",parentId:"); returnStr.append(node.getParentId()); returnStr.append(",children:["); List childList = getChildList(list,node); Iterator it = childList.iterator(); while(it.hasNext()){ Node n = (Node)it.next(); recursionFn(list,n); } returnStr.append("]},"); }else{ returnStr.append("{id:"); returnStr.append(node.getId()); returnStr.append(",parentId:"); returnStr.append(node.getParentId()); returnStr.append(",leaf:true},"); } } public boolean hasChild(List list, Node node){ //判断是否有子节点 return getChildList(list,node).size()>0?true:false; } public List getChildList(List list , Node node){ //得到子节点列表 List li = new ArrayList(); Iterator it = list.iterator(); while(it.hasNext()){ Node n = (Node)it.next(); if(n.getParentId()==node.getId()){ li.add(n); } } return li; } public String modifyStr(String returnStr){//修饰一下才能满足Extjs的Json格式 return ("["+returnStr+"]").replaceAll(",]", "]"); } public static void main(String[] args) { Recursion r = new Recursion(); r.recursionFn(r.nodeList, new Node(1,0)); System.out.println(r.modifyStr(r.returnStr.toString())); } }
Main方法运行效果如下:
[{id:1,parentId:0,children:[{id:2,parentId:1,children:[{id:4,parentId:2,leaf:true},{id:5,parentId:2,leaf:true},{id:6,par
entId:2,children:[{id:7,parentId:6,leaf:true},{id:8,parentId:6,leaf:true}]}]},{id:3,parentId:1,leaf:true}]}]
在具体的应用中稍加修改即可
本文章来自:http://emlyn1180.iteye.com/blog/315334
- ExtJs树的递归算法(Java),Json格式
- ExtJs树的递归算法(Java),Json格式
- ExtJs树的递归算法(Java),Json格式
- ExtJs树的递归算法(Java),Json格式
- java递归无级树输出json格式
- ExtJs加载JSON格式树
- Asp.Net 使用“递归算法”生成目录树的JSON格式(值得收藏)
- extjs递归算法生成树
- extjs json date 格式的完美解决方案
- Extjs树形嵌套json格式的拼接
- 为Extjs 树提供数据的递归算法
- Extjs 使用Ext.JSON.encode封装json格式的数据
- 用java的json-lib生成的JSON日期格式,如何让ExtJS显示?详细内容请看贴。
- Java递归算法构造JSON树形结构
- ExtJs与JSON格式数据绑定的方法(grid)
- ExtJs与JSON格式数据绑定的方法
- extjs Post方法提交json格式的数据到php
- C# 将DataTable转换成ExtJS可用的Json格式
- C6000系列DSP的EMIFA接口
- High Availability for the HDFS Namenode(翻译)
- 查看HP UNIX小型机的信息的命令集
- Apache Http Server和Tomcat 之区别
- linux关键系统服务详解
- ExtJs树的递归算法(Java),Json格式
- unix常用命令
- 哈夫曼编码 C#
- 关于用PIO读取Excel2003和2007的不同版本处理
- nysit 251—AMAZING AUCTION
- tomca下的web工程打包成war
- linux指令大全
- Android图像开源视图:SmartImageView
- something about IBM DB2