获得树形json串
来源:互联网 发布:免费手机视频制作软件 编辑:程序博客网 时间:2024/05/16 16:23
public class TreeNode {
private long nodeId;private String nodeName;
private long fatherNodeId;
public TreeNode() {
}
public TreeNode(long nodeId, String nodeName, long fatherNodeId) {
this.nodeId = nodeId;
this.nodeName = nodeName;
this.fatherNodeId = fatherNodeId;
}
public long getNodeId() {
return nodeId;
}
public void setNodeId(long nodeId) {
this.nodeId = nodeId;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public long getFatherNodeId() {
return fatherNodeId;
}
public void setFatherNodeId(long fatherNodeId) {
this.fatherNodeId = fatherNodeId;
}
}
public class ConstructorTree {
StringBuffer json = new StringBuffer();
/**
*
* @Title: getJson
* @Description: 调用接口
* @author Administrator
* @param list
* @param node
* @return
*/
public String getJson(List<TreeNode> list, TreeNode node) {
json = new StringBuffer();
constructorJson(list, node);
String jsonDate = json.toString();
return ("[" + jsonDate + "]").replaceAll(",]", "]");
}
/**
*
* @Title: constructorJson
* @Description: 构建json树
* @author Administrator
* @param list
* @param treeNode
*/
@SuppressWarnings("unchecked")
public void constructorJson(List<TreeNode> list, TreeNode treeNode) {
if (hasChild(list, treeNode)) {
json.append("{id:");
json.append(treeNode.getNodeId());
json.append(",text:");
json.append(treeNode.getNodeName());
json.append(",children:[");
List<TreeNode> childList = getChildList(list, treeNode);
Iterator iterator = childList.iterator();
while (iterator.hasNext()) {
TreeNode node = (TreeNode) iterator.next();
constructorJson(list, node);
}
json.append("]},");
} else {
json.append("{id:");
json.append(treeNode.getNodeId());
json.append(",text:");
json.append(treeNode.getNodeName());
json.append("},");
}
}
/**
*
* @Title: getChildList
* @Description: 获得子节点列表信息
* @author Administrator
* @param list
* @param node
* @return
*/
public List<TreeNode> getChildList(List<TreeNode> list, TreeNode node) {
List<TreeNode> li = new ArrayList<TreeNode>();
Iterator it = list.iterator();
while (it.hasNext()) {
TreeNode n = (TreeNode) it.next();
if (n.getFatherNodeId() == node.getNodeId()) {
li.add(n);
}
}
return li;
}
/**
*
* @Title: hasChild
* @Description: 判断是否有子节点
* @author Administrator
* @param list
* @param node
* @return
*/
public boolean hasChild(List<TreeNode> list, TreeNode node) {
return getChildList(list, node).size() > 0 ? true : false;
}
}
- 获得树形json串
- EasyUITree实现树形结构Json串
- httpURLConnection获得网络数据解析json串
- 遍历树形json
- java-树形json对象
- GET获得json
- 获得json数据
- get获得json---解析
- 获得json数据(表格)
- 构建Json树形目录结构
- 树形结构的json数据源,
- 通过表名获得列名信息,返回json串
- jquery_ajax get()获得json数据
- Jquery跨域获得Json
- Android获得json和XML
- easyui扁平Json生成树形菜单
- Java递归算法构造JSON树形结构
- java 树形结构转换为json字符串
- //优先队列的插入与删除操作
- 手势图片
- nginx 配置伪静态
- js中几种实用的跨域方法原理详解
- ftp、sftp常用命令
- 获得树形json串
- 购物节来啦!!!!!!!!!!!
- 【android】进程优先级(Framework设置优先级,LowMemoryKiller查杀)
- ApiDemos – BouncingBalls分析
- 语音编码分类及编解码标准
- shell编程--简单代码统计脚本
- web.xml里<filter-mapping>中的<dispatcher>作用
- OCP规范中一个有趣的AVP REPORTING REASON
- 对Java程序员有益的十个网站