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 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;
}
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】AI 的力量,开发者的翅膀:欢迎使用 AI 原生开发工具 TRAE
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
· InnoDB为什么不用跳表,Redis为什么不用B+树?
· 记一次 C# 平台调用中因非托管 union 类型导致的内存访问越界
· [EF Core]聊聊“复合”属性
· 那些被推迟的 C# 14 特性及其背后的故事
· 我最喜欢的 C# 14 新特性
· 博客园出海记-开篇:扬帆启航
· 微软开源的 MCP 教程「GitHub 热点速览」
· 记一次 .NET 某汽车控制焊接软件 卡死分析
· 关于布尔类型的变量不要加 is 前缀,被网友们吐槽了,特来完善下
· C#中的多级缓存架构设计与实现深度解析