如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//讀取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文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
{ "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文件及相應處理方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/superlover_/article/details/79461111