首先假設有這么一個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進去就行了。