获得树形json串
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串的更多相关文章
- iOS 字典或者数组和JSON串的转换
在和服务器交互过程中,会iOS 字典或者数组和JSON串的转换,具体互换如下: // 将字典或者数组转化为JSON串 + (NSData *)toJSONData:(id)theData { NSEr ...
- [原创] C# dynamic拼接Json串
using Newtonsoft.Json; 之前拼接两个json串,是用的这样的代码 , json1.Length - ); json2 = json2.Insert(json2 - , tmp); ...
- spring入门(七)【springMVC返回json串】
现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...
- java对象与json串互转
1:java对象与json串转换: java对象—json串: JSONObject JSONStr = JSONObject.fromObject(object); String str = JSO ...
- spring 4.x下让http请求返回json串
当前很多应用已经开始将响应返回为json串,所以基于springframework框架开发的服务端程序,让响应返回json字符串成为了一种常用手段. 这里介绍一下如何在spring-MVC框架下方便快 ...
- curl运行json串,代理转发格式
curl -b 'uin=o0450654733; skey=@tq9xjRvYy' -H "Content-Type: application/json" -X POST -d ...
- 使用FastJSON,将对象或数组和JSON串互转
Fastjson,是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库.其开源的下载网址为:https://github.com/AlibabaTech/fastjson. 示例代码如下: ...
- 利用QJSON将FDQuery转成JSON串
服务器要支持Http协议,打算采用Http+JSON的方式来交换数据.一开始考虑使用superobject,因为以前使用比较多,比较熟悉. 代码如下: class function FDQueryTo ...
- 怎么解析json串在.net中
以前知道一种解析json串的方法,觉得有点麻烦.就从别的地方搜到了另一种 string json = vlt.getlist(); JObject jo = JObject.Parse(json); ...
随机推荐
- AVL树----java
AVL树----java AVL ...
- IOS 轻量级数据持久化 DataLite
开发的过程中我们经常要保存一些配置信息,一般简单的是用 NSUserDefaults [[NSUserDefaults standardUserDefaults] objectForKey:key]; ...
- 记录路径dp-4713-Permutation
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4713 题目大意: 题意同HDU 3092这不过这题要输出路径. 解题思路: 思路同HDU 3092. ...
- Linux下套接字具体解释(三)----几种套接字I/O模型
參考: 网络编程–IO模型演示样例 几种server端IO模型的简介及实现 背景知识 堵塞和非堵塞 对于一个套接字的 I/O通信,它会涉及到两个系统对象.一个是调用这个IO的进程或者线程,还有一个就是 ...
- 用VC实现竖写汉字的方法
中国人自古就有自右至左.从上到下书写汉字的习惯.而当我们在自己所编写的应用程序中使用输出函数输出的总是自左至右的横排文字.有没有可能在我们的应用程序中实现竖写汉字的效果呢?笔者偶然发现了一种利用VC实 ...
- windows下eclipse跑junit报错:CreateProcess error=206
from:http://isuifengfei.iteye.com/blog/1684262 windows下,eclipse中运行junit出现错误提示: Exception occurred ex ...
- MPICH3环境配置
最新版的mpich简化了运行方式,不再提供mpd开头的命令,只需要一个mpiexec即可启动mpi运行环境,方便了mpi编程.源代码下载地址:http://www.mpich.org/download ...
- Android studio ElasticDownloadView
找到个开源项目,地址:https://github.com/Tibolte/ElasticDownload 下载进度效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkb ...
- Android开发调试日志工具类[支持保存到SD卡]
直接上代码: package com.example.callstatus; import java.io.File; import java.io.FileWriter; import java.i ...
- 窗体的Alpha通道透明色支持(一旦 Form 被定义为利用 LayeredWindow ,窗口的绘图不再响应沿用多年的 WM_Paint 消息)
参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...