I am building one app in which I request a PHP file from server. This PHP file returns a JSONArray having JSONObjects as its elements e.g.,
[ { "uniqid":"h5Wtd", "name":"Test_1", "address":"tst", "email":"[email protected]", "mobile":"12345", "city":"ind" }, {...}, {...}, ... ]
my code:
/* jArrayFavFans is the JSONArray i build from string i get from response. its giving me correct JSONArray */ JSONArray jArrayFavFans=new JSONArray(serverRespons); for (int j = 0; j < jArrayFavFans.length(); j++) { try { if (jArrayFavFans.getJSONObject(j).getString("uniqid").equals(id_fav_remov)) { //jArrayFavFans.getJSONObject(j).remove(j); //$ I try this to remove element at the current index... But remove doesn't work here ???? $ //int index=jArrayFavFans.getInt(j); Toast.makeText(getParent(), "Object to remove...!" + id_fav_remov, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
How do I remove a specific element from this JSONArray?
To remove JSON element, use the delete keyword in JavaScript.
To delete a JSON object from a list: Parse the JSON object into a Python list of dictionaries. Use the enumerate() function to iterate over the iterate over the list. Check if each dictionary is the one you want to remove and use the pop() method to remove the matching dict.
To remove JSON object key and value with JavaScript, we use the delete operator.
You can easy get a JSON array element by key like this: var value = ArrName['key_1']; //<- ArrName is the name of your array console. log(value);
Try this code
ArrayList<String> list = new ArrayList<String>(); JSONArray jsonArray = (JSONArray)jsonObject; if (jsonArray != null) { int len = jsonArray.length(); for (int i=0;i<len;i++){ list.add(jsonArray.get(i).toString()); } } //Remove the element from arraylist list.remove(position); //Recreate JSON Array JSONArray jsArray = new JSONArray(list);
Edit: Using ArrayList
will add "\"
to the key and values. So, use JSONArray
itself
JSONArray list = new JSONArray(); JSONArray jsonArray = new JSONArray(jsonstring); int len = jsonArray.length(); if (jsonArray != null) { for (int i=0;i<len;i++) { //Excluding the item at position if (i != position) { list.put(jsonArray.get(i)); } } }
In case if someone returns with the same question for Android platform, you cannot use the inbuilt remove()
method if you are targeting for Android API-18 or less. The remove()
method is added on API level 19. Thus, the best possible thing to do is to extend the JSONArray
to create a compatible override for the remove()
method.
public class MJSONArray extends JSONArray { @Override public Object remove(int index) { JSONArray output = new JSONArray(); int len = this.length(); for (int i = 0; i < len; i++) { if (i != index) { try { output.put(this.get(i)); } catch (JSONException e) { throw new RuntimeException(e); } } } return output; //return this; If you need the input array in case of a failed attempt to remove an item. } }
EDIT As Daniel pointed out, handling an error silently is bad style. Code improved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With