脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-获得树形json串,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
public class TreeNode {
PRivate long nodeid;}
public class ConstructorTree {
StringBuffer JSON = new StringBuffer();
/**
*
* @TITle: getJson
* @Description: 调用接口
* @author Administrator
* @param list
* @param node
* @return
*/
public String getJson(List
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
if (hasChild(list, treeNode)) {
json.apPEnd("{id:");
json.append(treeNode.getNodeId());
json.append(",text:");
json.append(treeNode.getNodeName());
json.append(",children:[");
List
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
List
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
return getChildList(list, node).size() > 0 ? true : false;
}
}
觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的javascript代码实例教程-获得树形json串全部内容,希望文章能够帮你解决javascript代码实例教程-获得树形json串所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。