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对象. 用Java,用什么实现在线聊天?dwr,coment。。。 Hibernate批量操作的事物问题,相当奇怪,大侠请进 请高手帮助啊 hibernate缓存问题 乱码 Eclipse+hibernate3问题 为什么这个Applet运行报错? jb怎样调试我单独装的TOMCAT下的web程序 100分,小问题,大家帮忙!!! Hql以关联对象的某一属性来进行查询? 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对象.