如下所示:
//讀取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讀取本地json文件及相應處理方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選