色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

JAVA如何轉(zhuǎn)換樹結(jié)構(gòu)數(shù)據(jù)代碼實例

瀏覽:121日期:2022-09-03 16:38:15

在實戰(zhàn)開發(fā)中經(jīng)常有需要處理樹形菜單、樹形目錄等等等業(yè)務(wù)需求。而對于這種產(chǎn)品,在設(shè)計數(shù)據(jù)庫時也建議使用id<----->parentId的結(jié)構(gòu)來做。但是最終前端顯示多用hightChart或者Echart插件來實現(xiàn)。所以在給前端數(shù)據(jù)時,最好的做法就是把數(shù)據(jù)庫結(jié)構(gòu)話的數(shù)據(jù)處理成treeJson格式。

第一步:引入fastjson

<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version></dependency>

第二步:用到了工具內(nèi)的JSONPath

JSONPath使用教程

/** * 樹轉(zhuǎn)換 * * @param obj 需要轉(zhuǎn)換的對象 * @param parentCodeFieldName 父標(biāo)識字段名 * @param parentCode 父標(biāo)識值 * @param currentCodeFieldName 當(dāng)前標(biāo)識字段名 * @param childrenFiledName 子樹的字段名 * @param c 需要轉(zhuǎn)換的Class類型 * @param <T> 泛型 * @return 返回List<T> */ public static <T> List<T> tree(Object obj, String parentCodeFieldName, String parentCode, String currentCodeFieldName, String childrenFiledName, Class<T> c) { long t1 = System.currentTimeMillis(); String jsonStr = JSON.toJSONString(obj); log.debug('樹轉(zhuǎn)換開始 >>>>>>>>>>>>>>>> {}', JSON.toJSONString(obj)); //獲取第一層級的數(shù)據(jù) JSONArray jsonArray = (JSONArray) JSONPath.read(jsonStr, '$[' + parentCodeFieldName + '=' + parentCode + ']'); if (CollectionUtils.isEmpty(jsonArray)) { //為空的話直接返回空集合 return Lists.newArrayList(); } for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String code = jsonObject.getString(currentCodeFieldName); treeChildren(jsonStr, jsonObject, parentCodeFieldName, code, currentCodeFieldName, childrenFiledName); } List<T> list = JSONArray.parseArray(jsonArray.toString(), c); log.debug('樹轉(zhuǎn)換結(jié)束, 轉(zhuǎn)換時間: {} ms . >>>>>>>>>>>>>>>> {}', (System.currentTimeMillis() - t1), JSON.toJSONString(list)); return list; } private static void treeChildren(String jsonStr, JSONObject currentJsonObj, String parentCodeFieldName, String parentCode, String currentCodeFieldName, String childrenFiledName) { JSONArray jsonArray = (JSONArray) JSONPath.read(jsonStr, '$[' + parentCodeFieldName + '=' + parentCode + ']'); if (CollectionUtils.isEmpty(jsonArray)) { return; } currentJsonObj.put(childrenFiledName, jsonArray); for (int i = 0; i < jsonArray.size(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String code = jsonObject.getString(currentCodeFieldName); treeChildren(jsonStr, jsonObject, parentCodeFieldName, code, currentCodeFieldName, childrenFiledName); } }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 韩国美女毛片 | 亚洲精品资源在线 | 午夜三级国产精品理论三级 | 欧美日韩一区二区三区在线视频 | 免费观看亚洲视频 | 久久tv免费国产高清 | 国产高清一区二区三区视频 | 特级毛片永久久免费观看 | 国产日韩欧美网站 | 大尺度福利视频奶水在线 | 色综合视频一区二区观看 | 成人国产一区 | 亚洲视频一区在线观看 | 国产东北色老头老太性视频 | 亚洲精品韩国美女在线 | 91热在线观看精品 | 久久久www成人免费精品 | 看片亚洲 | 国产成人黄网址在线视频 | 国产精品久久福利网站 | 99在线精品免费视频 | 看黄网址| 真实国产普通话对白乱子子伦视频 | 精品国产自在在线在线观看 | 成年人视频在线免费播放 | 欧美精品在线一区二区三区 | 亚洲三级网址 | 色老头oldmoneyvideos | 欧美毛片日韩一级在线 | 久久久久久极精品久久久 | 久草在线视频免费看 | 国产高清一级毛片在线不卡 | 黄色国产网站 | 欧美aaa| 成人三级做爰在线观看男女 | 亚洲国产成人久久综合一区77 | 欧美日韩黄色 | 美女张开腿让男人桶爽免费网站 | 91亚洲综合| 日本欧美亚洲 | 免费人成在线观看网站品爱网 |