0

i am using following import org.json.JSONArray

String name = "manjeet";
if (jsonArray.getJSONObject(i).get("name").equals(name))
{
jsonArray.remove(i);
}

if I am calling this code from main in a class it is working fine but when I am calling it from UI it is behaving differently.

it is able to get inside if but while executing .remove(i) it is giving

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;
manjeet lama
  • 185
  • 3
  • 12
  • as the error says there so such method as `JSONArray.remove(I)` , possible duplicate of https://stackoverflow.com/questions/8820551/how-do-i-remove-a-specific-element-from-a-jsonarray – warl0ck May 26 '17 at 12:42
  • I just want to know why this error is coming. The method is present there at compile time. – manjeet lama May 29 '17 at 13:49
  • you can loop through your jsonarray and put the values you want in the `new arraylist` since there is no such method as `jsonArray.remove ` you are confusing it with javascript – warl0ck May 30 '17 at 05:56
  • I am already using what you are suggesting and the API I am using 'org.json.JSONArray' does have remove(I) method. Anyways thanks for your suggestion. – manjeet lama May 30 '17 at 06:46
  • I have added an answer also for reference. – warl0ck May 30 '17 at 06:59

2 Answers2

0

To remove particular element from your jsonArray you will have to create an Arraylist and store the values you want from the jsonArray instead.

You can implement it like:

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.

Notice the ! in if condition.

warl0ck
  • 3,356
  • 4
  • 27
  • 57
0

Problem: jar conflict

It may happen sometime that same class is provided by multiple jar's.

In my case org.json.JSONArray is also present in 'jackson-datatype-json-org' but that is having an older version of the class, in which remove method is not available.

Due to this the 'remove' method was avilable at compile time or while running the same code from main, but when I was running it from UI it was picking the class from 'jackson-datatype-json-org'.

So that is the reason I was getting java.lang.NoSuchMethodError exception.

Solution: we can use exclusions tag in dependency('jackson-datatype-json-org') to remove the class that we don't want to use.

Example:

<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>
manjeet lama
  • 185
  • 3
  • 12