错误信息
java.lang.NoSuchMethodError: org.json.JSONArray.isEmpty()Z
我的场景
我尝试使用 org.json
JAR 中 JSONArray
的 isEmpty()
方法。
我使用 IntelliJ 作为我的 IDE。我没有在我的代码中使用任何接近反射的东西。我声明了一个 JSONArray
,并在几行之后尝试检查它是否为空,但是在尝试使用 isEmpty()
时出现此错误。
我确实通过使用 jsonArray.length() > 0
解决了这个问题,但错误消息让我着迷。我查看了 JSONArray 的反编译 .class 文件,方法 isEmpty()
exists,并且有一个public access修饰符。此外,InteliiJ 建议我在输入代码时可以使用 isEmpty()
。那么是什么导致了这个错误的发生呢?
代码示例
JSONArray filesJsonArray = new JSONArray();
JSONObject fileObject = new JSONObject();
fileObject.put("fileId",fileId);
fileObject.put("fileName",fileName);
fileObject.put("mimeType",mimeType);
filesJsonArray.put(fileObject);
if (!filesJsonArray.isEmpty()){ //the condition check here throws the error.
}
(为简洁起见忽略了 Catch block )
我的导入
在我使用这段代码的类(class)中,这是我所有的导入
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.sql.*;
import java.util.Base64;
最佳答案
如果你使用的是spring boot,那么你可以试试这个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</exclusion>
</exclusions>
</dependency>
它对我有用..
关于java - NoSuchMethodError 在 org.json 中使用 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418646/