如何解决JSONParser()的错误?

时间:2015-10-24 18:21:49

标签: java json intellij-idea

我正在尝试使用java从json文件中读取。这就是我到目前为止所做的:

 import jdk.nashorn.internal.parser.JSONParser;
 import java.io.FileReader;
 import java.util.Objects;

 public static void main (String[] args){
      JSONParser parser = new JSONParser(); //Error : JSONParser() in  JSONParser cannot be applied to expected parameters ...        
        System.out.println("Reading JSON file ...");
        FileReader fileReader = new FileReader("data.json"); // *file not found exception!
        JSONObject json = (JSONObject) parser.parse(fileReader); //JSONParser cannot be applied to java.io.File.Reader
        String t1 = (String) json.get("time");
        System.out.println("Time: " + t1);
   }

我在本节中有以下错误:

  Error:(14, 29) java: constructor JSONParser in class       jdk.nashorn.internal.parser.JSONParser cannot be applied to given types;
 required: java.lang.String,jdk.nashorn.internal.objects.Global,boolean
 found: no arguments
 reason: actual and formal argument lists differ in length

我在目录中有文件data.json,但它无法读取它并给我FileNotFoundException

我发现了以下几行:

  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;

它也给了我以下这两个错误:

Error:(2, 23) java: package org.json.simple does not exist
Error:(3, 30) java: package org.json.simple.parser does not exist

我正在使用IntelliJ IDEA 14.1.4。我该如何解决这些错误?

1 个答案:

答案 0 :(得分:0)

import jdk.nashorn.internal.parser.JSONParser此解析器不是您正在寻找的解析器。 本教程使用import org.json.simple.parser.JSONParser;,这要求您通过将此依赖项添加到Maven项目来添加正确的包:

<groupid>com.googlecode.json-simple</groupid>
<artifactid> json-simple</artifactid>
<version>1.1</version>

或者在本教程中使用正确的jar文件 json-simple-1.1.1.jar