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

如下所示:

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都网站建设、滦南网络推广、小程序定制开发、滦南网络营销、滦南企业策划、滦南品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供滦南建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

//读取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> btnMap = new HashMap<>();
  Iterator 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文件及相应处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。


网站栏目:Java读取本地json文件及相应处理方法
网页地址:http://scyanting.com/article/jhegpd.html