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

您的位置:首頁技術文章
文章詳情頁

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

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

在實戰(zhàn)開發(fā)中經(jīng)常有需要處理樹形菜單、樹形目錄等等等業(yè)務需求。而對于這種產(chǎn)品,在設計數(shù)據(jù)庫時也建議使用id<----->parentId的結構來做。但是最終前端顯示多用hightChart或者Echart插件來實現(xiàn)。所以在給前端數(shù)據(jù)時,最好的做法就是把數(shù)據(jù)庫結構話的數(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 父標識字段名 * @param parentCode 父標識值 * @param currentCodeFieldName 當前標識字段名 * @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)換結束, 轉(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)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美成人性色大片在线观看 | 亚洲精品久久99久久一区 | 中文字幕日韩一区二区不卡 | 国产精品九九 | 亚洲国产一区二区三区综合片 | 久久国内免费视频 | 美国免费三片在线观看 | 国产大片一区 | 视频一区免费 | 亚洲综合网址 | 久久亚洲精品中文字幕亚瑟 | 国产午夜一级淫片 | 日本一区二区高清免费不卡 | 国产孕妇孕交一级毛片 | 久久精品国产欧美日韩亚洲 | 国产孕妇孕交视频在线观看 | 未成人做爰视频www 窝窝午夜精品一区二区 | 天天五月天丁香婷婷深爱综合 | 99久久伊人一区二区yy5099 | 久久久久毛片成人精品 | 最爽的乱淫片免费 | 99久久精品费精品国产一区二区 | 91精品国产91久久 | 亚洲日本在线观看网址 | 久久不色| 中国japanesevideo乱 | 日韩视频大全 | 午夜精品久视频在线观看 | 免费观看a黄一级视频 | 午夜限制r级噜噜片一区二区 | 成在线人免费视频 | 久草网站在线 | 狠狠色狠狠色综合 | 美女三级毛片 | 国产成人亚洲精品91专区高清 | 中文字幕一区二区在线播放 | 欧美精品免费看 | 精品日本久久久久久久久久 | 99久久亚洲 | 成人a级 | 国产黄毛片 |