简体   繁体   English

无法在Java中读取json文件

[英]Cannot read json file in java

I am using eclipse for reading a jsonfile. 我正在使用eclipse读取jsonfile。 I put the jsonfile in my src->main->java->testjson->jsonfile.json . 我将jsonfile放在src-> main-> java-> testjson-> jsonfile.json中。 Now I am trying to read the jsonfile. 现在,我正在尝试读取jsonfile。 But my progam cannot find the file. 但是我的程序无法找到该文件。 I get the output "nothing". 我得到的输出为“ nothing”。 Here is the code I already implement: 这是我已经实现的代码:

JsonParser parser = new JSONParser();
try{

Object obj = parser.parse(new FileReader("jsonfile.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");

}

catch(Exception e){
System.out.println("nothing");
}

As you're saying that you use Eclipse, i assume you also run your code via Eclipse. 正如您所说的那样,您使用Eclipse,我假设您还通过Eclipse运行代码。 As a default, the working directory when executing a Java program in Eclipse is the root folder of the project. 默认情况下,在Eclipse中执行Java程序时的工作目录是项目的根文件夹。 Therefore, I suggest to put your jsonfile.json in the root folder of your project instead of src/main/... . 因此,我建议将jsonfile.json放在项目的根文件夹中,而不是src/main/...

Furthermore, you should not catch Exception . 此外,您不应捕获Exception Catch more specific like IOException or JSONException and then print the exception message ( e.getMessage() ), then it is much easier to solve the problem. 捕获更具体的信息(例如IOExceptionJSONException ,然后输出异常消息( e.getMessage() ),则可以轻松解决问题。

Your file within the project is called a "resource", which will be bundled in the resulting jar -file. 您在项目内的文件称为“资源”,它将捆绑在生成的jar -file中。

In maven projects such files resides in a special folder resources (like src/main/resources/testjson/jsonfile.json ), in many other project types, these files are located directly beneath the java files. maven项目中,此类文件驻留在特殊的文件夹resources (例如src/main/resources/testjson/jsonfile.json ),在许多其他项目类型中,这些文件位于java文件的正下方。

Therefore you cannot read it with FileReader , because it will not be a regular file, but zipped inside the jar file. 因此,您不能使用FileReader读取它,因为它不是常规文件,而是压缩在jar文件中。

All you have to do is to read the file with this.getClass().getResourceAsStream("/testjson/jsonfile.json") . 您所需要做的就是使用this.getClass().getResourceAsStream("/testjson/jsonfile.json")读取文件。

Your parser should be capable to read from an InputStream instead of a Reader . 您的解析器应该能够从InputStream而不是Reader If not, utilize an InputStreamReader with the correct encoding (JSON files should be UTF-8, but that depends...) 如果不是,请使用具有正确编码的InputStreamReader (JSON文件应为UTF-8,但这取决于...)

Code: 码:

 try (InputStream is = this.getClass().getResourceAsStream("/testjson/jsonfile.json"); ) {
     Object obj = parser.parse(is); 
 } catch (Exception ex) {
     System.out.println("failed to read: "+ex.getMessage());
 }

Code if parser does not support InputStream : 解析器不支持InputStream代码:

 try (InputStream is = this.getClass().getResourceAsStream("/testjson/jsonfile.json"); 
     Reader rd = new InputStreamReader(is, "UTF-8"); ) {
     Object obj = parser.parse(rd); 
 } catch (Exception ex) {
     System.out.println("failed to read: "+ex.getMessage());
 }

Give the full path to the JSON file instead of the file name. 提供JSON文件的完整路径,而不是文件名。

If the file path is home/src/main/java/testjson/jsonfile.json 如果文件路径是home / src / main / java / testjson / jsonfile.json

String path = "home/src/main/java/testjson/jsonfile.json";
Object obj = parser.parse(new FileReader(path));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM