错误消息
java.lang.NoSuchMethodError: org.json.JSONArray.isEmpty()Z
我的情景
我尝试使用来自isEmpty() JAR的JSONArray的JSONArray方法。
我正在使用IntelliJ作为我的IDE。在我的代码中,我没有使用任何接近反射的东西。我声明了一个JSONArray,在一些行试图检查它是否为空之后,但是当我尝试使用isEmpty()时会得到这个错误。
我确实是通过使用jsonArray.length() > 0来解决这个问题的,但是错误信息正是让我着迷的原因。我查看了未编译的.class文件JSONArray,方法isEmpty() 存在,并具有公共访问修饰符。此外,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块因简洁而被排除)
我的进口品
在我使用这段代码的类中,这是我所有的导入
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;发布于 2019-09-16 10:38:23
如果您使用的是spring引导,那么您可以尝试如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</exclusion>
</exclusions>
</dependency>它对我有效..。
发布于 2021-01-22 21:40:33
对于那些还在研究这个问题的人来说,主要的问题是JSONAssert。
要确认,可以使用任何依赖图工具来验证JSONAssert是否添加了“android”。
maven的例子:
mvn dependency:treeandroid提供了它自己的org.json.*对象的实现,这些对象并不是最新的。因此,将它们排除为JSONAssert依赖项。
maven的例子:
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>这应该可以让您在使用JSONAssert的伟大输出的同时避免这个问题!
发布于 2021-06-29 21:16:04
您需要从spring starter中清除artifactId依赖项的包,下面是如何清除任何依赖项(考虑到我使用的是spring starter 2.4.4)。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</exclusion>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
<exclusion>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</exclusion>
</exclusions>
</dependency>https://stackoverflow.com/questions/54418646
复制相似问题