Java读取本地json文件及相应处理方法
时间:2020-11-12 08:38:48|栏目:JAVA代码|点击: 次
如下所示:
//读取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": "删除"
}
]
}
]
}
上一篇:java判断String类型是否能转换为int的方法
栏 目:JAVA代码
下一篇:基于springMvc+hibernate的web application的构建
本文地址:http://www.codeinn.net/misctech/21831.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




