2013-01-31 19 views
0
符號錯誤

[說明]
1.使用Java
2.使用org.json.JSONArray和org.json.JSONObject
[問題]
當我打電話JSONArray的remove()方法,編譯我的項目時總是得到「找不到符號:方法remove(int)」,對此有何幫助?JSONArray刪除無法找到JAVA

這裏有一個類似的問題:How to remove JSONArray element using Java
但答案似乎是不正確的,因爲這將直接刪除裏面的JSONObject的鍵值對而不是整個insinde的JSONObject。

示例代碼:

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. 

預先感謝任何幫助。

+0

你使用(說)舊版本的沒有特定方法的庫? –

+0

[檢查這個] [1] 它可能對你有幫助...使用名稱值刪除JSON數組 [1]:http://stackoverflow.com/questions/6310623/remove-item -from-json-array-using-its-name-value –

+0

[給Brian]我檢查了我使用的jar文件,它有刪除方法。 [給Pratik]謝謝,但我說我使用java,而不是javascript。 –

回答

1

好了,有趣的是,在中央Maven的最新org.json JAR確實包含一個JSONArray有remove方法。這是的javap對類提取物,從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; 

這編譯代碼與現有的source codeofficial JSON.org site不一致,所以我使用它。圖書館是這麼死的簡單,我建議乾脆抓自己,要麼源:

  • 編譯成一個JAR
  • 包括直接到你的項目。
+0

感謝感。 在我的項目中,我反編譯org.json.jar,它有一個remove()方法。不知道爲什麼得到這個「找不到符號」的錯誤。 現在我使用ArrayList 而不是JSONArray,然後我可以使用ArrayList的remove()方法。 再次感謝您的幫助和建議。 –