Java 在Spring项目中从文本文件构造JSON对象
我想知道是否有一种方法可以从spring项目中的resources文件夹中读取文本文件,并将文本文件内容转换为Java 我尝试过使用下面的代码读取文件并将对象转换为JSONobject,但没有成功Java 在Spring项目中从文本文件构造JSON对象,java,json,spring,Java,Json,Spring,我想知道是否有一种方法可以从spring项目中的resources文件夹中读取文本文件,并将文本文件内容转换为Java 我尝试过使用下面的代码读取文件并将对象转换为JSONobject,但没有成功 ClassLoader classLoader = ResourceLoader.class.getClassLoader(); File file = new File(classLoader.getResource("input.txt").getFile()); JSONParser json
ClassLoader classLoader = ResourceLoader.class.getClassLoader();
File file = new File(classLoader.getResource("input.txt").getFile());
JSONParser jsonParser = new JSONParser("input.txt");
感谢谷歌json的工作
JSONParser parser = new JSONParser();
InputStream pdInputStream =
this.getClass().getResourceAsStream("/test.json");
Reader reader = new InputStreamReader(pdInputStream);
JSONObject a = (JSONObject) parser.parse(reader);
使用以下maven依赖项
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
com.googlecode.json-simple
简单json
1.1.1