Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.json.JSONArray';s remove方法正在给出NoSuchMethodError异常_Java_Json - Fatal编程技术网

Java org.json.JSONArray';s remove方法正在给出NoSuchMethodError异常

Java org.json.JSONArray';s remove方法正在给出NoSuchMethodError异常,java,json,Java,Json,我正在使用以下import org.json.JSONArray String name = "manjeet"; if (jsonArray.getJSONObject(i).get("name").equals(name)) { jsonArray.remove(i); } 如果我在一个类中从main调用这段代码,它工作正常,但是当我从UI调用它时,它的行为不同 它可以在执行的时候进入,移除(i)它所给予的 com.sun.jersey.spi.container.ContainerRes

我正在使用以下import org.json.JSONArray

String name = "manjeet";
if (jsonArray.getJSONObject(i).get("name").equals(name))
{
jsonArray.remove(i);
}
如果我在一个类中从main调用这段代码,它工作正常,但是当我从UI调用它时,它的行为不同

它可以在执行的时候进入,移除(i)它所给予的

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not 
be mapped to a response, re-throwing to the HTTP container
java.lang.NoSuchMethodError: org.json.JSONArray.remove(I)Ljava/lang/Object;

要从
jsonArray
中删除特定元素,您必须创建
Arraylist
并存储想要从jsonArray中获得的值

您可以像这样实现它:

String name = "manjeet";
ArrayList<String> final_list = new ArrayList<>();
if (! jsonArray.getJSONObject(i).get("name").equals(name))
{
    final_list.add(i);
}
// now you can use the final_list instead of using jsonArray.
String name=“manjeet”;
ArrayList final_list=新的ArrayList();
如果(!jsonArray.getJSONObject(i).get(“name”).equals(name))
{
最终清单。添加(i);
}
//现在,您可以使用final_列表,而不是使用jsonArray。

注意
,则在
中进行编码>

问题:jar冲突

有时可能会出现由多个jar提供相同类的情况

在我的例子中,org.json.JSONArray也出现在“jackson datatype json org”中,但该类有一个较旧的版本,其中remove方法不可用

因此,“remove”方法在编译时或从main运行相同代码时是可用的,但当我从UI运行它时,它是从“jackson datatype json org”中选取类

这就是我得到java.lang.NoSuchMethodError异常的原因

解决方案:我们可以在dependency('jackson-datatype-json-org')中使用Exclutions标记来删除我们不想使用的类

示例:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-json-org</artifactId>
  <version>2.8.8</version>
  <exclusions>
    <exclusion>
      <groupId>org.apache.geronimo.bundles</groupId>
      <artifactId>json</artifactId>
    </exclusion>
  </exclusions>
</dependency>

com.fasterxml.jackson.datatype
jackson数据类型json-org
2.8.8
org.apache.geronimo.bundles
json

正如错误所说,有这样一种方法,如
JSONArray.remove(I)
,可能是重复的,我只想知道为什么会出现此错误。该方法在编译时存在。您可以循环使用jsonarray,并将所需的值放入
新arraylist
中,因为没有像
jsonarray.remove
这样的方法,您将其与javascriptI混淆。我已经在使用您建议的方法,并且我正在使用的API“org.json.jsonarray”确实有remove(I)方法。不管怎样,谢谢你的建议。我已经添加了一个答案也供参考。