如下所示:
//读取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");
//将读取的数据转换为JSON
JSON json = JSON .from (input);
if (json != null) {
//取出按钮权限的数据
btnArray = json .getJSONArray("btnList");
}
Map<String, List<MenuVo>> btnMap = new HashMap<>();
Iterator< > num = btnArray.iterator();
//遍历JSONArray,转换格式。按按钮集合按模块(name)放入map中
while (num.hasNext()) {
JSON btn = (JSON ) 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": "删除"
}
]
}
]
}
以上这篇Java读取本地json文件及相应处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
-
带你读《深入理解AutoML和AutoDL:构建自动化机器 学习与深度学习平台》之二:自动化人工智能
2026-05-16栏目: 教程
-
深度融合,POLARDB与SuperMap联合构建首个云原生时空平台
2026-05-16栏目: 教程
-
带你读《Java图像处理:基于OpenCV与JVM》之一:基于JavaVM的OpenCV
2026-05-16栏目: 教程
-
这样才能正确解锁MaxCompute客户端
2026-05-16栏目: 教程
-
API经济下,如何玩转API?
2026-05-16栏目: 教程
