Java 格式化json為json樹


/**
     * @param treeList      數據
     * @param parentIdValue 父級值
     * @param parentIdFiled 父級字段名稱
     * @param idFiled       子級字段名稱
     * @Description json轉jsonTree
     * @Author dongjd
     * @Date 19-1-11 下午5:38
     * @Return com.alibaba.fastjson.JSONArray
     */
    public static JSONArray jsonTreeFormat(List<Map<String, Object>> treeList, String parentIdValue, String parentIdFiled, String idFiled) {
        JSONArray childMenu = new JSONArray();
        treeList.stream().map(map -> JSONObject.parseObject(JSON.toJSONString(map))).forEach(jsonMenu -> {
            String menuId = jsonMenu.getString(idFiled);
            String pid = jsonMenu.getString(parentIdFiled);
            if (parentIdValue.equals(pid)) {
                JSONArray childList = jsonTreeFormat(treeList, menuId, parentIdFiled, idFiled);
                jsonMenu.put("childList", childList);
                childMenu.add(jsonMenu);
            }
        });
        return childMenu;
    }

參考資料[1]:https://blog.csdn.net/jianxia801/article/details/82771346

/**
* @param treeList
* @param parentIdValue 級值
* @param parentIdFiled 字段名
* @param idFiled 字段名
* @Description jsonjsonTree
* @Author dongjd
* @Date 19-1-11 下午5:38
* @Return com.alibaba.fastjson.JSONArray
*/
public static JSONArray jsonTreeFormat(List<Map<String, Object>> treeList, String parentIdValue, String parentIdFiled, String idFiled) {
JSONArray childMenu = new JSONArray();
treeList.stream().map(map -> JSONObject.parseObject(JSON.toJSONString(map))).forEach(jsonMenu -> {
String menuId = jsonMenu.getString(idFiled);
String pid = jsonMenu.getString(parentIdFiled);
if (parentIdValue.equals(pid)) {
JSONArray childList = jsonTreeFormat(treeList, menuId, parentIdFiled, idFiled);
jsonMenu.put("childList", childList);
childMenu.add(jsonMenu);
}
});
return childMenu;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM