代码之家  ›  专栏  ›  技术社区  ›  Daniel B.

在org.json中使用jsonarray时出现nosuchmethoderror

  •  1
  • Daniel B.  · 技术社区  · 7 年前

    错误消息

    java.lang.NoSuchMethod错误:org.json.jsonarray.isEmpty()z

    我的剧本

    我试着用 isEmpty() 方法 JSONArray org.json 罐子。

    我用intellij作为我的ide。我的代码中没有使用任何接近反射的东西。我宣布 JSON阵列 ,在一些行尝试检查它是否为空之后,但是在尝试使用 小精灵 .

    我只是通过使用 jsonArray.length() > 0 ,但这个错误信息让我着迷。我查了一下 jsonarray的文件和方法 iSimple() 存在 并且有一个 公共访问修饰符 . 另外,英特利伊建议我可以用 iSimple() 当输入代码时。那么是什么导致了这个错误的发生呢?

    代码示例

    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 blocks被取消)

    我的进口货

    在我使用这段代码的类中,这是我所有的导入

    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;
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Naya    7 年前

    我相信你加入了你的计划

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

    因为你好像混淆了两类: org.json.JSONArray 具有 org.json.simple.JSONArray . 第一个没有那个方法,第二个有。 下面是这两个类的代码示例:

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

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

    因此,作为解决方案,我建议清理并重建项目。

        2
  •  0
  •   Alexey Simonov    7 年前

    在添加到项目后,我遇到了同样的错误:

    <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>
    

    它不见了。