Android中对JSONArray数组的指定项进行删除,更新。

首先假设有这么一个JSONArray

JSONArray Array1;
JSONArray ITEM = new JSONArray();

name为你获取要删除的字段名称,IETM就是你删除后得到的新的JSONArray数组了

          String name = null;
                try {
                         name = orderDish.getString("name");
                         JSONObject odItems = null;     
                          for(int j=0;j<Array1.length();j++)
                          {
                              odItems = (JSONObject)Array1.get(j);
                             if(name.equals(odItems.getString("name")))
                             {
                                Array1.put(j, null);
//                                 continue;
                             }
                             else
                             {
                                 newItems.put(odItems);
                             }
                          }
                    
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

同理,替换就是直接把要替换的给put进去就行了。

原文地址:https://www.cnblogs.com/LIANQQ/p/3157657.html