Java讀取本地json文件及相應(yīng)處理方法
更新時(shí)間:2018年09月12日 11:16:29 作者:superlovelei
今天小編就為大家分享一篇Java讀取本地json文件及相應(yīng)處理方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
//讀取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");
//將讀取的數(shù)據(jù)轉(zhuǎn)換為JSONObject
JSONObject jsonObject = JSONObject.fromObject(input);
if (jsonObject != null) {
//取出按鈕權(quán)限的數(shù)據(jù)
btnArray = jsonObject.getJSONArray("btnList");
}
Map<String, List<MenuVo>> btnMap = new HashMap<>();
Iterator<Object> num = btnArray.iterator();
//遍歷JSONArray,轉(zhuǎn)換格式。按按鈕集合按模塊(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": "授權(quán)"
},
{
"id": "jsgldelete",
"name": "刪除"
}
]
}
]
}
以上這篇Java讀取本地json文件及相應(yīng)處理方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決異常處理問題:getReader()?has?already?been?called?for?this
這篇文章主要介紹了解決異常處理:getReader()?has?already?been?called?for?this問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
IDEA創(chuàng)建MyBatis配置文件模板的方法步驟
這篇文章主要介紹了IDEA創(chuàng)建MyBatis配置文件模板的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Mybatis分頁的實(shí)現(xiàn)及使用注解開發(fā)操作
這篇文章主要介紹了Mybatis分頁的實(shí)現(xiàn)及使用注解開發(fā)操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
微信游戲打飛機(jī)游戲制作(java模擬微信打飛機(jī)游戲)
java模擬微信打飛機(jī)游戲,大家參考使用吧2013-12-12

