如下所示:
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