这篇文章主要给大家介绍了关于利用java读取web项目中json文件为map集合的相关资料,文中通过示例代码给大家介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
前言
本文主要介绍了关于java读取web项目中json文件为map集合的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
实例介绍
假设当前项目web目录(/resource/test.json)下有一json文件如下:
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
立即学习“Java免费学习笔记(深入)”;
[
{
"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();
}以上就是关于Java如何读取web项目中json文件为map集合的方法的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号