Java读取本地json文件及相应处理方法
作者:superlovelei 时间:2023-10-16 16:37:34
如下所示:
//读取json文件地址
/* String path = getClass().getClassLoader().getResource("menu.json").toString();
path = path.replace("\\", "/");
if (path.contains(":")) {
path = path.replace("file:/", "");
}*/
ClassPathResource resource = new ClassPathResource("menu.json");
File filePath = resource.getFile();
JSONArray btnArray = null;
//读取文件
String input = FileUtils.readFileToString(filePath, "UTF-8");
//将读取的数据转换为JSONObject
JSONObject jsonObject = JSONObject.fromObject(input);
if (jsonObject != null) {
//取出按钮权限的数据
btnArray = jsonObject.getJSONArray("btnList");
}
Map<String, List<MenuVo>> btnMap = new HashMap<>();
Iterator<Object> num = btnArray.iterator();
//遍历JSONArray,转换格式。按按钮集合按模块(name)放入map中
while (num.hasNext()) {
JSONObject btn = (JSONObject) num.next();
btnMap.put((String) btn.get("name"), JSONArray.toList((JSONArray) btn.get("children"), new MenuVo(), new JsonConfig()));
}
json文件
{
"btnList": [
{
"name": "用户管理",
"children": [
{
"id": "yhgladd",
"name": "添加"
},
{
"id": "yhgledit",
"name": "编辑"
},
{
"id": "yhgldelete",
"name": "暂停"
},
{
"id": "yhglstart",
"name": "启用"
},
{
"id": "yhglsee",
"name": "查看"
}
]
},
{
"name": "角色管理",
"children": [
{
"id": "jsgladd",
"name": "添加"
},
{
"id": "jsgledit",
"name": "编辑"
},
{
"id": "jsglauth",
"name": "授权"
},
{
"id": "jsgldelete",
"name": "删除"
}
]
}
]
}
来源:https://blog.csdn.net/superlover_/article/details/79461111
标签:Java,json
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java web实现账号单一登录,防止同一账号重复登录(踢人效果)
2021-05-24 13:26:09
![](https://img.aspxhome.com/file/2023/9/75789_0s.png)
Android开机画面的具体修改方法
2023-05-13 20:52:22
java使用Jsoup组件生成word文档
2022-10-28 12:44:16
c# 如何实现代码生成器
2023-11-13 19:23:35
![](https://img.aspxhome.com/file/2023/0/94540_0s.png)
Java数据结构与算法之选择排序(动力节点java学院整理)
2022-06-23 12:28:24
![](https://img.aspxhome.com/file/2023/5/93065_0s.png)
浅谈Async和Await如何简化异步编程(几个实例让你彻底明白)
2021-07-28 22:41:48
![](https://img.aspxhome.com/file/2023/4/104284_0s.jpg)
SpringBoot实现文件上传下载功能小结
2022-11-09 07:40:07
Android在自定义类中实现自定义监听器方式
2023-06-17 08:37:34
Android实现颜色渐变动画效果
2022-05-31 09:52:53
![](https://img.aspxhome.com/file/2023/8/117128_0s.gif)
java精度计算代码 java指定精确小数位
2023-07-31 03:03:58
![](https://img.aspxhome.com/file/2023/4/71004_0s.jpg)
C++实现的O(n)复杂度内查找第K大数算法示例
2023-06-30 15:51:13
Java 判断一个时间是否在另一个时间段内
2023-02-24 18:44:25
浅谈idea live template高级知识_进阶(给方法,类,js方法添加注释)
2023-12-07 22:50:28
java GUI编程之监听操作实例分析
2022-09-28 05:55:53
分析Android内存泄漏的几种可能
2022-06-01 21:55:48
![](https://img.aspxhome.com/file/2023/1/110721_0s.png)
SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解决方案
2021-08-26 12:54:32
![](https://img.aspxhome.com/file/2023/5/130025_0s.png)
java中的final关键字详解及实例
2022-03-02 11:26:59
解析ScrollView--仿QQ空间标题栏渐变
2021-06-20 22:44:54
![](https://img.aspxhome.com/file/2023/6/98656_0s.gif)
C#多线程实现异步接口
2023-09-09 04:58:52
![](https://img.aspxhome.com/file/2023/5/116495_0s.jpg)
SpringCloud中Eureka的配置及使用讲解
2022-04-19 19:16:28
![](https://img.aspxhome.com/file/2023/1/70351_0s.png)