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.

8
  • Are you using (say) an old version of the lib that doesn't have that particular method ? Commented Jan 31, 2013 at 12:49
  • [Check this][1] Its may Helpful to You... Remove json array using name-value [1]: stackoverflow.com/questions/6310623/… Commented Jan 31, 2013 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. Commented Jan 31, 2013 at 13:01
  • Hmmm, the remove(int) method has been in the codebase since the very first commit. This has to be an error in how you included the code into your project. Commented Jan 31, 2013 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. Commented Jan 31, 2013 at 13:03

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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<JSONObject> instead of JSONArray, then I can use ArrayList's remove() method. Thanks again for your help and advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.