java - NoSuchMethodError 在 org.json 中使用 JSONArray

标签 java json

错误信息

java.lang.NoSuchMethodError: org.json.JSONArray.isEmpty()Z

我的场景

我尝试使用 org.json JAR 中 JSONArrayisEmpty() 方法。

我使用 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/

相关文章:

javascript - 使用 JSON 数据在 HTML 中填充下拉列表

python - 加载json文件时如何将所有列表转换为集合

javascript - 如何在 Angular 中绑定(bind)多个插值模板?

java - 为什么 Eclipse 插件开发中的 Properties load 方法失​​败?

java - OpenJPA 和存储过程、Weblogic 12c

至少有 10 个字符的长整数的 java String 格式

json - 使用标准 JSON-RPC 实现的优势

javascript - 使用 AJAX/JSON 返回整个文件

java - 在第二个 Activity 中显示按钮CheckAnswer

java - JMX 使用 Jetty 作为嵌入式服务器启用我的应用程序