Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a specific element from a JSONArray?

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?

like image 961
Rupesh Yadav Avatar asked Jan 11 '12 14:01

Rupesh Yadav


People also ask

How do you remove an element from a JSON file?

To remove JSON element, use the delete keyword in JavaScript.

How do you remove an element from a JSON object in Python?

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.

How do you remove a key value pair from a JSON object?

To remove JSON object key and value with JavaScript, we use the delete operator.

How get values from JsonArray?

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);


2 Answers

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));         }    }  } 
like image 112
Vinothkumar Arputharaj Avatar answered Sep 24 '22 01:09

Vinothkumar Arputharaj


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.

like image 27
C-- Avatar answered Sep 22 '22 01:09

C--