如何在Android中删除JSON数组元素

时间:2021-05-08 21:21:50
 { "Employee" :  
        [  
         {"id":"101","name":"Sonoo Jaiswal","salary":"50000"},  
         {"id":"102","name":"Vimal Jaiswal","salary":"60000"}  
        ]   
    }  

for (int i =0;i< json_array.length(); i++) {
     json_array.remove(i);
  }

I have one sample JSONArray data. I want to remove the whole json array with certain condition. But json_array.remove(position) method is not working. Is there any other way to remove the whole JSON array or element in Android?

我有一个示例JSONArray数据。我想在一定条件下删除整个json数组。但是json_array.remove(position)方法不起作用。有没有其他方法可以删除Android中的整个JSON数组或元素?

3 个解决方案

#1


1  

If you iterate forwards through an array by index, removing elements, you will always miss the element after the one you have just removed.

如果通过索引向前遍历数组,删除元素,那么在您刚删除元素后,您将始终错过该元素。

Think about the array [A, B, C]:

想想数组[A,B,C]:

for (int i = 0; i < arr.length; ++i) {
  arr.remove(i);
}
  • On the first iteration, i == 0, so arr.remove(i) will remove A; the array is now [B, C]
  • 在第一次迭代中,i == 0,所以arr.remove(i)将删除A;数组现在是[B,C]

  • On the second iteration, i == 1, so arr.remove(i) will remove C; the array is now [B].
  • 在第二次迭代中,i == 1,因此arr.remove(i)将删除C;数组现在是[B]。

  • On the third iteration, i == 2, which is beyond the length of the array, so the loop stops.
  • 在第三次迭代中,i == 2,超出了数组的长度,因此循环停止。

Your final array is [B], not [].

你的最后一个数组是[B],而不是[]。

Iterate in reverse:

反向迭代:

 for (int i =json_array.length() - 1;i >= 0; i--) {
   json_array.remove(i);
 }

#2


0  

I want to remove the whole json array with certain condition

我想在一定条件下删除整个json数组

Then no need to use for-loop for removing items from JSONArray, just remove whole Employee JSONArray from JSONObject using Employee key:

然后,不需要使用for循环从JSONArray中删除项目,只需使用Employee键从JSONObject中删除整个Employee JSONArray:

JSONObject jsonObject=new JSONObject(<json_string>);
jsonObject.remove("Employee"); //<< pass JSONArray name here

#3


0  

Below is another you can do this

下面是另一个你可以做到这一点

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));
        }
    } 
    //Remove the element from arraylist
    list.remove(position);
}

#1


1  

If you iterate forwards through an array by index, removing elements, you will always miss the element after the one you have just removed.

如果通过索引向前遍历数组,删除元素,那么在您刚删除元素后,您将始终错过该元素。

Think about the array [A, B, C]:

想想数组[A,B,C]:

for (int i = 0; i < arr.length; ++i) {
  arr.remove(i);
}
  • On the first iteration, i == 0, so arr.remove(i) will remove A; the array is now [B, C]
  • 在第一次迭代中,i == 0,所以arr.remove(i)将删除A;数组现在是[B,C]

  • On the second iteration, i == 1, so arr.remove(i) will remove C; the array is now [B].
  • 在第二次迭代中,i == 1,因此arr.remove(i)将删除C;数组现在是[B]。

  • On the third iteration, i == 2, which is beyond the length of the array, so the loop stops.
  • 在第三次迭代中,i == 2,超出了数组的长度,因此循环停止。

Your final array is [B], not [].

你的最后一个数组是[B],而不是[]。

Iterate in reverse:

反向迭代:

 for (int i =json_array.length() - 1;i >= 0; i--) {
   json_array.remove(i);
 }

#2


0  

I want to remove the whole json array with certain condition

我想在一定条件下删除整个json数组

Then no need to use for-loop for removing items from JSONArray, just remove whole Employee JSONArray from JSONObject using Employee key:

然后,不需要使用for循环从JSONArray中删除项目,只需使用Employee键从JSONObject中删除整个Employee JSONArray:

JSONObject jsonObject=new JSONObject(<json_string>);
jsonObject.remove("Employee"); //<< pass JSONArray name here

#3


0  

Below is another you can do this

下面是另一个你可以做到这一点

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));
        }
    } 
    //Remove the element from arraylist
    list.remove(position);
}