软件编程
位置:首页>> 软件编程>> java编程>> Java读取本地json文件及相应处理方法

Java读取本地json文件及相应处理方法

作者:superlovelei  发布时间:2023-10-16 16:37:34 

标签:Java,json

如下所示:


//读取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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com