假设当前项目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()
}