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

xiaoxiao2021-02-28  137

假设当前项目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(); }
转载请注明原文地址: https://www.6miu.com/read-39118.html

最新回复(0)