java 将树形数据转换为json格式??? 本帖最后由 nie_peng 于 2010-09-07 18:05:56 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://falchion.javaeye.com/blog/187548 想请教具体的代码,关键是那个children 那一块怎么弄,如何循环得到下面的所以children? jQuery EasyUI插件有个异步树适合这种需求 public void getEasyTree(String parentId){ this.id = parentId; this.getEasyTreei(parentId); json = "[" + json.substring(0, json.length() - 1) + "]"; } public void getEasyTreei(String parentId) { childrenId = treeService.getChildrenId(parentId); if(childrenId != null){ json += "{ \"id\":\"" + parentId + "\""; json += ", \"text\":\"" + treeService.getName(parentId) + "\""; if(parentId == this.id) json += ", \"state\":\"open\""; else if(childrenId.size() > 0) json += ", \"state\":\"closed\""; json += ", \"attributes\":{\"departid\":\"" + parentId + "\"}"; if(childrenId.size() > 0){ json += ", \"children\":["; for (String child : childrenId){ this.getEasyTreei(child); } json = json.substring(0, json.length() - 1); json += "]"; } json += "},"; } } 这种拼法太累,而且如果值中有双引号的话,你浏览器端使用eval解析不出JSON对象。使用xml也许会更简单些,然后写个函数把xml转换成json对象. 如何取得下拉列表的值 struts2 Ztree半勾选怎么解决 通过wsdl生成webservice客户端问题 想请教下深入的问题。。。 关于JSP提取数据库时间问题。高手帮忙! 为什么要调两次interrupte 熟悉struts和上传组件的请进 初学java,想了解一下有哪些Java的集成开发环境,以及哪个或哪些比较好 再问部署war文件的问题 jsp连bean 为什么连不上呢,总说找不到bean? ubuntu 如何手工安装ssh 关于ibaits动态配置的问题,请教!
this.id = parentId;
this.getEasyTreei(parentId);
json = "[" + json.substring(0, json.length() - 1) + "]";
}
public void getEasyTreei(String parentId) {
childrenId = treeService.getChildrenId(parentId);
if(childrenId != null){
json += "{ \"id\":\"" + parentId + "\"";
json += ", \"text\":\"" + treeService.getName(parentId) + "\"";
if(parentId == this.id)
json += ", \"state\":\"open\"";
else if(childrenId.size() > 0)
json += ", \"state\":\"closed\"";
json += ", \"attributes\":{\"departid\":\"" + parentId + "\"}"; if(childrenId.size() > 0){
json += ", \"children\":[";
for (String child : childrenId){
this.getEasyTreei(child);
}
json = json.substring(0, json.length() - 1);
json += "]";
}
json += "},";
}
}
使用xml也许会更简单些,然后写个函数把xml转换成json对象.