有时候页面需要使用jQuery easy ui中的combotree,需要给combotree提供一个JSON数据,使用如下方法(递归)实现(下面是dao层的实现层):

    /**
* 根据表名和父id拼装树形JSON数据
* @param tableName
* @param parentId
* @return
* @throws Exception
*/
@Override
public String createTreeJsonBytableNameAndParentId(String tableName,String parentId) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); String sql = " select * from " + tableName + " where 1 = 1 ";
if (StringUtils.isNotEmpty(parentId)) {
sql = sql + " and parent_id = '" + parentId + "'";
}else {
sql = sql + " and parent_id is null";
}
Session session = getSession();
Query query = session.createSQLQuery(sql);
//存储过程键值对应
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List<Map<String,String>> lists = query.list();
for (Map<String,String> msp : lists) {
map = new HashMap<String, Object>();
List<Map<String, Object>> list_children = new ArrayList<Map<String,Object>>();
String id = msp.get("id");
map.put("id",msp.get("code"));
map.put("text",msp.get("name"));
list_children = createTreeChildJson(tableName,id);
if (list_children.size()>0) {
map.put("state","closed");
map.put("children", list_children);
}
list.add(map);
}
JSONArray arry = JSONArray.fromObject(list);
return arry.toString();
} /**
* 根据表名和父id拼装树形LIST数据递归
* @param tableName
* @param parentId
* @return
* @throws Exception
*/
private List<Map<String, Object>> createTreeChildJson(String tableName,String parentId) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); String sql = " select * from " + tableName + " where 1 = 1 ";
if (StringUtils.isNotEmpty(parentId)) {
sql = sql + " and parent_id = '" + parentId + "'";
}else {
sql = sql + " and parent_id is null";
}
Session session = getSession();
Query query = session.createSQLQuery(sql);
//存储过程键值对应
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List<Map<String,String>> lists = query.list();
for (Map<String,String> msp : lists) {
map = new HashMap<String, Object>();
List<Map<String, Object>> list_children = new ArrayList<Map<String,Object>>();
String id = msp.get("id");
map.put("id",msp.get("code"));
map.put("text",msp.get("name"));
list_children = createTreeChildJson(tableName,id);
if (list_children.size()>0) {
map.put("state","closed");
map.put("children", list_children);
}
list.add(map);
}
return list; }

拼装好的数据如下:

[
{
"id": "09DE010000",
"text": "漳卫新河",
"state": "closed",
"children": [
{
"id": "09DE010100",
"text": "四女寺减河"
},
{
"id": "09DE010200",
"text": "岔河"
},
{
"id": "09DE010300",
"text": "六五河"
}
]
},
{
"id": "09DE020000",
"text": "马颊河",
"state": "closed",
"children": [
{
"id": "09DE020100",
"text": "笃马河",
"state": "closed",
"children": [
{
"id": "09DE020101",
"text": "赵王河"
}
]
},
{
"id": "09DE020200",
"text": "二股河"
},
{
"id": "09DE020300",
"text": "朱龙河"
},
{
"id": "09DE020400",
"text": "德惠新河",
"state": "closed",
"children": [
{
"id": "09DE020401",
"text": "幸福河"
},
{
"id": "09DE020402",
"text": "商东河"
},
{
"id": "09DE020403",
"text": "临商河"
}
]
}
]
},
{
"id": "09DE030000",
"text": "沾利河"
},
{
"id": "09DE040000",
"text": "秦口河",
"state": "closed",
"children": [
{
"id": "09DE040100",
"text": "清坡河"
},
{
"id": "09DE040200",
"text": "沟盘河"
}
]
},
{
"id": "09DE050000",
"text": "徒骇河",
"state": "closed",
"children": [
{
"id": "09DE050100",
"text": "沙河"
},
{
"id": "09DE050200",
"text": "潘庄总干渠"
},
{
"id": "09DE050300",
"text": "温聪河"
},
{
"id": "09DE050400",
"text": "倪伦河"
},
{
"id": "09DE050500",
"text": "老赵牛河"
},
{
"id": "09DE050600",
"text": "赵牛河",
"state": "closed",
"children": [
{
"id": "09DE050601",
"text": "中心河"
},
{
"id": "09DE050602",
"text": "巴公河"
}
]
},
{
"id": "09DE050700",
"text": "苇河"
},
{
"id": "09DE050800",
"text": "土马河"
}
]
},
{
"id": "09DE060000",
"text": "大沙河"
},
{
"id": "09DE070000",
"text": "淄脉河"
},
{
"id": "09DE080000",
"text": "小清河",
"state": "closed",
"children": [
{
"id": "09DE080100",
"text": "杏花河"
},
{
"id": "09DE080200",
"text": "孝妇河"
},
{
"id": "09DE080300",
"text": "绣河"
},
{
"id": "09DE080400",
"text": "淄河"
}
]
},
{
"id": "09DE090000",
"text": "新塌河",
"state": "closed",
"children": [
{
"id": "09DE090100",
"text": "织女河"
},
{
"id": "09DE090200",
"text": "北阳河"
}
]
},
{
"id": "09DE100000",
"text": "弥河",
"state": "closed",
"children": [
{
"id": "09DE100100",
"text": "尧河"
}
]
},
{
"id": "09DE110000",
"text": "白浪河"
},
{
"id": "09DE120000",
"text": "虞河"
},
{
"id": "09DE130000",
"text": "潍河",
"state": "closed",
"children": [
{
"id": "09DE130100",
"text": "汶河"
},
{
"id": "09DE130200",
"text": "渠河"
}
]
},
{
"id": "09DE140000",
"text": "五龙河",
"state": "closed",
"children": [
{
"id": "09DE140100",
"text": "富水河"
},
{
"id": "09DE140200",
"text": "清水河"
},
{
"id": "09DE140300",
"text": "砚河"
}
]
},
{
"id": "09DE150000",
"text": "崂山水库"
},
{
"id": "09DE160000",
"text": "墨水河"
},
{
"id": "09DE170000",
"text": "王河"
},
{
"id": "09DE180000",
"text": "界河"
},
{
"id": "09DE190000",
"text": "黄水河",
"state": "closed",
"children": [
{
"id": "09DE190100",
"text": "内夹河"
}
]
},
{
"id": "09DE200000",
"text": "大沽夹河",
"state": "closed",
"children": [
{
"id": "09DE200100",
"text": "清阳河"
},
{
"id": "09DE200200",
"text": "门楼水库"
}
]
},
{
"id": "09DE210000",
"text": "老母猪河",
"state": "closed",
"children": [
{
"id": "09DE210100",
"text": "母猪河"
}
]
},
{
"id": "09DE220000",
"text": "黄垒河"
},
{
"id": "09DE230000",
"text": "乳山河"
},
{
"id": "09DE240000",
"text": "大沽河",
"state": "closed",
"children": [
{
"id": "09DE240100",
"text": "小沽河"
},
{
"id": "09DE240200",
"text": "五沽河"
},
{
"id": "09DE240300",
"text": "潴河"
},
{
"id": "09DE240400",
"text": "胶莱河",
"state": "closed",
"children": [
{
"id": "09DE240401",
"text": "胶河"
},
{
"id": "09DE240402",
"text": "泽河"
}
]
}
]
}
]

数据库结构如下:

第一级,parent_id为null

第二级,parent_id为第一级数据的id

第三级,parent_id为第二级数据的id

......


以上仅供参考

java递归算法实现拼装树形JSON数据的更多相关文章

  1. 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化

    接着来说这个JSON循环引用的问题: 关于JSON格式的转化,其实关键就是这几个依赖: <!-- json --> <!-- 1号 --> <dependency> ...

  2. 在java中像js那样处理json数据

    工作中经常需要通过ajax向前台返回json数据,都是通过拼字符串拼出来的,很发麻烦不说,还容易出错. 于是想,能不能像js那样操作json呢?或者说更方便的操作呢? Google的gson就是这样的 ...

  3. java android使用Gson解析泛型json数据

    那就直接开始吧. 在我们获取服务器返回的json数据有时候会出现这种情况,比如: {"body":{"attrName":"feed",&q ...

  4. java中的lis数组转为json数据

    第一个想到的办法就是 javascript中的replace 也就是先将list数组转为 字符串再对 字符串  replace 但是万万没想到javascript的replace函数在替换数据时, 默 ...

  5. 将相关数据拼成所需JSON数据

    参考: http://www.cnblogs.com/shuilangyizu/p/6019561.html 有时候我们需要将一些数据拼装成所需要格式的JSON,可以使用如下方法,本人觉得还是比较方便 ...

  6. poi导出excel,表头数据动态拼装

    /* * 第一步:拼装表头和数据 */ // 放多个sheet的集合 List<Map<String,Object>> datas = new ArrayList<Map ...

  7. Java创建和解析Json数据方法(二)——org.json包的使用

    (二)org.json包的使用 1.简介   工具包org.json.jar,是一个轻量级的,JAVA下的json构造和解析工具包,它还包含JSON与XML, HTTP headers, Cookie ...

  8. Java创建和解析Json数据方法——org.json包的使用(转)

    org.json包的使用 1.简介   工具包org.json.jar,是一个轻量级的,JAVA下的json构造和解析工具包,它还包含JSON与XML, HTTP headers, Cookies, ...

  9. Spring MVC返回Map格式JSON数据

    问题描述: ajax中走error : function(e) {} 问题背景: 在测试controller层时,试过了ResponseEntity<ResponseModel>这种类型返 ...

随机推荐

  1. APP H5页面显示优化

    在开发移动端APP页面时,对各操作系统各种型号的手机进行适配是必须的.然鹅,上周在开发完一个落地页后,被测试给打了回来,其中列出了一个在我看来很小的问题:单击进入页面的时候,页面还没加载完的时候字体显 ...

  2. SpringSecurity3整合CAS实现单点登录

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. Eclipse搭建Gradle环境

    转自:http://blog.sina.com.cn/s/blog_4b20ae2e0102uz4t.html 1.上Grandle官网下载Gradle,地址:http://www.gradle.or ...

  4. windows服务与计划任务

    1. 打开Visual Studio 2012新建一个project Solution: 2. 选择Windows->windows Service,修改服务名称:MyFirstService: ...

  5. feedparser win7 python 安装

    要获取订阅源读取当中的内容,Universal Feed Parser提供了非常多的API,这里说下详细的配置. 我的配置环境: win7 + python 2.7.5 步骤: 1.下载feedpar ...

  6. UVA 11107(Life Forms-后缀数组+二分)

    Problem C: Life Forms You may have wondered why most extraterrestrial life forms resemble humans, di ...

  7. 漏洞预警 | ECShop全系列版本远程代码执行高危漏洞

    2018年9月1日,阿里云态势感知发布预警,近日利用ECShop全系列版本的远程代码执行漏洞进行批量化攻击量呈上升趋势.该漏洞利用简单且危害较大,黑客可通过WEB攻击直接获得服务器权限. 漏洞原理 该 ...

  8. 对于Ian的访谈,不少关于GAN的内容

    文章链接如下: http://3g.163.com/dy/article/DD1GBSLF0511ABV6.html 里面提到胶囊网络,我找了这篇文章看了下: https://blog.csdn.ne ...

  9. easyui更换主题之后出现validatebox的验证提示信息显示跑偏的解决方案

    在easyui中更换主题为非default的主题,有的主题会出现如下图所示的情况,验证提示信息的提示内容跑到了下面. 现在说下原因和解决方案: 原因:原因是easyui对某些主题没有进行这个样式的设置 ...

  10. TextView文字横向自己主动滚动

              效果截图:                              watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjI4Nj ...