This article mainly introduces you to the relevant information about using java to read json files in web projects into map collections. The article introduces you to you in great detail through sample code, which has certain reference for your study or work. Value, friends in need come and take a look below.
Preface
This article mainly introduces the relevant content about Java reading json files in web projects into map collections, and shares them for your reference. Learning, I won’t say much more below, let’s take a look at the detailed introduction.
Instance introduction
Assume that there is a json file in the current project web directory (/resource/test.json) as follows:
[ { "path": "content_111", "title": "文章1", "imgUrl": "../../../libs/img/pptau/pf.jpg" }, { "path": "content_222", "title": "文章2", "imgUrl": "../../../libs/img/pptau/pf.jpg" } ]
The method to read it as List
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(); }
The above is the detailed content of About how Java reads json files in web projects into map collections. For more information, please follow other related articles on the PHP Chinese website!