java构造json树形结构_JAVA 根据数据库表内容生产树结构JSON数据

1 {2 "checked":false,3 "children":[4 {5 "checked":false,6 "children":[7 {8 "checked":false,9 "children":[10 {11 "checked":false,12 "children":[13

14 ],15 "id":"12",16 "parent":true,17 "parentId":"3",18 "state":"open",19 "text":"String"20 },21 {22 "checked":false,23 "children":[24

25 ],26 "id":"18",27 "parent":true,28 "parentId":"3",29 "state":"open",30 "text":"等等"31 }32 ],33 "id":"3",34 "parent":true,35 "parentId":"1",36 "state":"open",37 "text":"JAVA"38 },39 {40 "checked":false,41 "children":[42 {43 "checked":false,44 "children":[45

46 ],47 "id":"13",48 "parent":true,49 "parentId":"4",50 "state":"open",51 "text":"sql"52 },53 {54 "checked":false,55 "children":[56

57 ],58 "id":"19",59 "parent":true,60 "parentId":"4",61 "state":"open",62 "text":"等等"63 }64 ],65 "id":"4",66 "parent":true,67 "parentId":"1",68 "state":"open",69 "text":"oracle"70 },71 {72 "checked":false,73 "children":[74 {75 "checked":false,76 "children":[77

78 ],79 "id":"14",80 "parent":true,81 "parentId":"5",82 "state":"open",83 "text":"ioc"84 },85 {86 "checked":false,87 "children":[88

89 ],90 "id":"15",91 "parent":true,92 "parentId":"5",93 "state":"open",94 "text":"aop"95 },96 {97 "checked":false,98 "children":[99

100 ],101 "id":"20",102 "parent":true,103 "parentId":"5",104 "state":"open",105 "text":"等等"106 }107 ],108 "id":"5",109 "parent":true,110 "parentId":"1",111 "state":"open",112 "text":"spring"113 },114 {115 "checked":false,116 "children":[117

118 ],119 "id":"6",120 "parent":true,121 "parentId":"1",122 "state":"open",123 "text":"springmvc"124 },125 {126 "checked":false,127 "children":[128

129 ],130 "id":"7",131 "parent":true,132 "parentId":"1",133 "state":"open",134 "text":"fastdfs"135 },136 {137 "checked":false,138 "children":[139

140 ],141 "id":"8",142 "parent":true,143 "parentId":"1",144 "state":"open",145 "text":"linux"146 },147 {148 "checked":false,149 "children":[150

151 ],152 "id":"16",153 "parent":true,154 "parentId":"1",155 "state":"open",156 "text":"等等"157 }158 ],159 "id":"1",160 "parent":true,161 "parentId":"0",162 "state":"open",163 "text":"技术学习"164 },165 {166 "checked":false,167 "children":[168 {169 "checked":false,170 "children":[171

172 ],173 "id":"9",174 "parent":true,175 "parentId":"2",176 "state":"open",177 "text":"骑行"178 },179 {180 "checked":false,181 "children":[182

183 ],184 "id":"10",185 "parent":true,186 "parentId":"2",187 "state":"open",188 "text":"吃喝玩乐"189 },190 {191 "checked":false,192 "children":[193

194 ],195 "id":"11",196 "parent":true,197 "parentId":"2",198 "state":"open",199 "text":"学习"200 },201 {202 "checked":false,203 "children":[204

205 ],206 "id":"17",207 "parent":true,208 "parentId":"2",209 "state":"open",210 "text":"等等"211 }212 ],213 "id":"2",214 "parent":true,215 "parentId":"0",216 "state":"open",217 "text":"兴趣"218 }219 ],220 "id":"0",221 "parent":false,222 "parentId":"",223 "state":"open",224 "text":"关于本人"225 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java解析json字符串。 commons-beanutils-1.9.0 commons-collections-3.2.1 commons-lang-2.6 commons-logging-1.1.3 ezmorph-1.0.6 json-lib-2.4-jdk15 demo: package com; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.ezmorph.object.DateMorpher; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.JSONUtils; public class Jsontest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JSONObject jsonObj = new JSONObject(); jsonObj.put("name", "hzj"); jsonObj.put("sex", "female"); System.out.println(jsonObj); } public static Object jsonToBean(String jsonString, Class cla) { JSONObject jsonObj = null; try { setDateFormat2Java(); jsonObj = JSONObject.fromObject(jsonString); } catch (Exception ex) { ex.printStackTrace(); } return JSONObject.toBean(jsonObj, cla); } public static Object jsonToBean(String jsonString, Class cla, Map map) { JSONObject jsonObj = null; try { setDateFormat2Java(); jsonObj = JSONObject.fromObject(jsonString); } catch (Exception ex) { ex.printStackTrace(); } return JSONObject.toBean(jsonObj, cla, map); } public static Object[] jsonToArray(String jsonString, Class cla) { Object[] arrObj = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); arrObj = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); arrObj[i] = JSONObject.toBean(jsonObject, cla); } } catch (Exception ex) { ex.printStackTrace(); } return arrObj; } public static Object[] jsonToArray(String jsonString, Class cla, Map map) { Object[] arrObj = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); arrObj = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); arrObj[i] = JSONObject.toBean(jsonObject, cla, map); } } catch (Exception ex) { ex.printStackTrace(); } return arrObj; } public static List jsonToList(String jsonString, Class cla) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ArrayList(); for (Iterator iter = array.iterator(); iter.hasNext();) { JSONObject jsonObject = (JSONObject) iter.next(); list.add(JSONObject.toBean(jsonObject, cla)); } } catch (Exception ex) { ex.printStackTrace(); } return list; } public static List jsonToList(String jsonString, Class cla, Map map) { List list = null; try { setDateFormat2Java(); JSONArray array = JSONArray.fromObject(jsonString); list = new ArrayList(); for (Iterator iter = array.iterator(); iter.hasNext();) { JSONObject jsonObject = (JSONObject) iter.next(); list.add(JSONObject.toBean(jsonObject, cla, map)); } } catch (Exception ex) { ex.printStackTrace(); } return list; } public static Map jsonToMap(String jsonString) { Map map = null; try { setDateFormat2Java(); JSONObject jsonObject = JSONObject.fromObject(jsonString); map = new HashMap(); for (Iterator iter = jsonObject.keys(); iter.hasNext();) { String key = (String) iter.next(); map.put(key, jsonObject.get(key)); } } catch (Exception ex) { ex.printStackTrace(); } return map; } public static Object[] jsonToArray(String jsonString) { JSONArray jsonArray = JSONArray.fromObject(jsonString); return jsonArray.toArray(); } public static void setDateFormat2Java() { JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" })); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值