JAVA如何轉(zhuǎn)換樹(shù)結(jié)構(gòu)數(shù)據(jù)代碼實(shí)例
在實(shí)戰(zhàn)開(kāi)發(fā)中經(jīng)常有需要處理樹(shù)形菜單、樹(shù)形目錄等等等業(yè)務(wù)需求。而對(duì)于這種產(chǎn)品,在設(shè)計(jì)數(shù)據(jù)庫(kù)時(shí)也建議使用id<----->parentId的結(jié)構(gòu)來(lái)做。但是最終前端顯示多用hightChart或者Echart插件來(lái)實(shí)現(xiàn)。所以在給前端數(shù)據(jù)時(shí),最好的做法就是把數(shù)據(jù)庫(kù)結(jié)構(gòu)話的數(shù)據(jù)處理成treeJson格式。
第一步:引入fastjson
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version></dependency>
第二步:用到了工具內(nèi)的JSONPath
JSONPath使用教程
/** * 樹(shù)轉(zhuǎn)換 * * @param obj 需要轉(zhuǎn)換的對(duì)象 * @param parentCodeFieldName 父標(biāo)識(shí)字段名 * @param parentCode 父標(biāo)識(shí)值 * @param currentCodeFieldName 當(dāng)前標(biāo)識(shí)字段名 * @param childrenFiledName 子樹(shù)的字段名 * @param c 需要轉(zhuǎn)換的Class類(lèi)型 * @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('樹(shù)轉(zhuǎn)換開(kāi)始 >>>>>>>>>>>>>>>> {}', JSON.toJSONString(obj)); //獲取第一層級(jí)的數(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('樹(shù)轉(zhuǎn)換結(jié)束, 轉(zhuǎn)換時(shí)間: {} 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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JS中6個(gè)對(duì)象數(shù)組去重的方法2. Python基于requests庫(kù)爬取網(wǎng)站信息3. 資深程序員:給Python軟件開(kāi)發(fā)測(cè)試的25個(gè)忠告!4. Python使用Selenium自動(dòng)進(jìn)行百度搜索的實(shí)現(xiàn)5. 一文帶你徹底理解Java序列化和反序列化6. python中文本字符處理的簡(jiǎn)單方法記錄7. PHP利用curl發(fā)送HTTP請(qǐng)求的實(shí)例代碼8. PHP laravel實(shí)現(xiàn)導(dǎo)出PDF功能9. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法10. Java commons-httpclient如果實(shí)現(xiàn)get及post請(qǐng)求

網(wǎng)公網(wǎng)安備