java读取web项目下json文件为map集合

来源:互联网 发布:第一版主网小说网网络 编辑:程序博客网 时间:2024/06/06 08:59

假设当前项目web目录(/resource/test.json)下有一json文件如下:

[  {    "path": "content_111",    "title": "文章1",    "imgUrl": "../../../libs/img/pptau/pf.jpg"  },    {    "path": "content_222",    "title": "文章2",    "imgUrl": "../../../libs/img/pptau/pf.jpg"  }]

其在java中读取为List<Map> 的方法为:

String dir = request.getSession().getServletContext().getRealPath("/resource/test.json");        try {            File file = new File(dir);            if (!file.exists()) {                file.createNewFile();            }           String str= FileUtils.readFileToString(file, "UTF-8");           List<Map> maps= (List)JSONArray.fromObject(str);        } catch (IOException e) {            e.printStackTrace();        }
原创粉丝点击