0

[Description]
1. Using Java
2. Use org.json.JSONArray and org.json.JSONObject
[Problem]
When I call JSONArray's remove() method, always get "cannot find symbol: method remove(int)" when compile my project, any help for this?

Here is a similar question: How to remove JSONArray element using Java
but the answer seems not correct, because that will just remove the inside JSONObject's key-value pair but not the whole insinde JSONObject.

Example code:

JSONArray test_arr = new JSONArray("[{'id':'1', 'name': 'name1'},{'id':'2', 'name':'name2'}]");
test_arr.remove(1);  // here will cause the "cannot find symbol" error.

Thanks in advance for any help.

Community
  • 1
  • 1
ZZ Shao
  • 93
  • 1
  • 6
  • Are you using (say) an old version of the lib that doesn't have that particular method ? – Brian Agnew Jan 31 '13 at 12:49
  • [Check this][1] Its may Helpful to You... Remove json array using name-value [1]: http://stackoverflow.com/questions/6310623/remove-item-from-json-array-using-its-name-value – Pratik Butani Jan 31 '13 at 12:51
  • [to Brian] I checked the jar file which I use, it do has remove method. [to Pratik] Thanks, but I said I use java, not javascript. – ZZ Shao Jan 31 '13 at 13:01
  • Hmmm, the `remove(int)` method has been in the codebase since the [very first commit](https://github.com/douglascrockford/JSON-java/commit/667813de3cbdcd0d39459499ad283fd03937d89d). This has to be an error in how you included the code into your project. – Perception Jan 31 '13 at 13:02
  • [to Perception] I use java, so ==> include "org.json.jar" in library path, and "import org.json.JSONArray;" in code. Other functions of this class works well in my project, but only this remove() function. – ZZ Shao Jan 31 '13 at 13:03
  • Make sure that the function exists in your jar, you can use this [tool](http://java.decompiler.free.fr/) or any other – NightWhisper Jan 31 '13 at 13:11
  • [to NightWhisper] Yes, I already checked, it do have. – ZZ Shao Jan 31 '13 at 13:13
  • Mentioning that you were using a JAR was quite useful, see my answer below. Also, to notify a SO user, put an @ in front of their name (instead of typing [to Whoever]). – Perception Jan 31 '13 at 14:37
  • @Perception Got it, thanks. – ZZ Shao Jan 31 '13 at 14:46

1 Answers1

1

Well, interestingly, the latest org.json JAR in Maven central indeed contains a JSONArray class that does not have a remove method. This is an extract of javap on the class, as extracted from json-20090211.jar:

public org.json.JSONArray put(int, long) throws org.json.JSONException;
public org.json.JSONArray put(int, java.util.Map) throws org.json.JSONException;
public org.json.JSONArray put(int, java.lang.Object) throws org.json.JSONException;
public org.json.JSONObject toJSONObject(org.json.JSONArray) throws org.json.JSONException;
public java.lang.String toString();
public java.lang.String toString(int) throws org.json.JSONException;
java.lang.String toString(int, int) throws org.json.JSONException;
public java.io.Writer write(java.io.Writer) throws org.json.JSONException;

This compiled code is inconsistent with the source code available from the official JSON.org site, so I would not use it. The library is so dead simple, I would recommend simply grabbing the source yourself and either:

  • compiling it into a JAR
  • including it directly into your project.
Perception
  • 75,573
  • 19
  • 170
  • 185
  • Thanks Perception. In my project, i decompile the org.json.jar, it DO has a remove() method. Have no idea about why got this "cannot find symbol" error. Now I use ArrayList instead of JSONArray, then I can use ArrayList's remove() method. Thanks again for your help and advice. – ZZ Shao Jan 31 '13 at 14:45