簡體   English   中英

NoSuchMethodError 在 org.json 中使用 JSONArray

[英]NoSuchMethodError using JSONArray in org.json

錯誤信息

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

我的場景

我嘗試使用org.json JAR 中的JSONArrayisEmpty()方法。

我使用 IntelliJ 作為我的 IDE。 我沒有在我的代碼中使用任何接近反射的東西。 我聲明了一個JSONArray ,並在嘗試檢查它是否為空的幾行之后,但是在嘗試使用isEmpty()時出現此錯誤。

我只是通過使用jsonArray.length() > 0解決了這個問題,但錯誤消息讓我着迷。 我查看了 JSONArray 的反編譯.class文件,方法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;

如果你使用的是 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>

它對我有用..

我相信你添加到你的項目中

compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'

因為您似乎混淆了 2 個類: org.json.JSONArrayorg.json.simple.JSONArray 第一個沒有這種方法,第二個有。 以下是兩個類的代碼示例:

https://www.programcreek.com/java-api-examples/org.json.JSONArray

https://www.programcreek.com/java-api-examples/org.json.simple.JSONArray

因此,作為解決方案,我建議清理並重建項目。

添加到我的項目后,我遇到了同樣的錯誤:

<dependency>    
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-json-org</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
  <version>2.9.2</version>
</dependency>

之后,我清理並更新了依賴項

<dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-json-org</artifactId>
            <version>2.9.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.json</groupId>
                    <artifactId>json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

它不見了。

對於仍在關注此問題的任何人,主要問題在於 JSONAssert。

要確認,請使用您必須使用的任何依賴關系圖工具來驗證 JSONAssert 是否添加了“android-json”。

Maven 示例:

mvn dependency:tree

android-json 提供了它自己的 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 的出色輸出!

您需要從spring-boot-starter-test artifactId清除org.json依賴包,這里是如何清除任何依賴(考慮到我使用的是spring-boot-starter-parent 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM