获得树形json串
2015-12-27 09:45
543 查看
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;
}
}
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;
}
}
相关文章推荐
- JavaScript构造函数详解
- 【转】页面加载等待页面JS
- js追加事件执行代码场景
- JavaScript生成二维码图片小结
- 良好的异常展现界面<500.jsp>
- js中location.search、split()HTML5中localStorage
- Javascript removeChild()删除节点及删除子节点的方法
- JavaScript生成二维码图片小结
- JavaScript构造函数详解
- JavaScript的removeChild()函数用法详解
- ocx使用时js报错"对象不支持的属性或方法"
- [LeetCode][JavaScript]Create Maximum Number
- js中的innerHTML和outerHTML区别
- js转换金钱为中文单位元、万元、亿元、万亿
- JavaScript中的对象及层级关系、常用函数
- ExtJs设置文本框是否可用
- JSP页面添加当前时间
- javascript之判断专题
- JavaScript:理解parseInt
- JavaScript-----语法