2016-09-05 15 views
1

私はアンドロイドアプリケーションとアプリケーションのどこかで作業しています.JsonArrayからいくつかのJsonObjectを削除したいと思います。以下はJsonArrayは次のとおりです。JsonArrayからいくつかのキー値に基づいてJsonobjectを削除してください。android

[ 
    { 
    "id": 2, 
    "image": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/e0/39/01/137a8_8640.png?c=279d", 
    }, 
    { 
    "id": 4, 
    "image": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/5f/3a/01/13826_c4c9.png?c=ff74", 
    }, 
    { 
    "id": 1, 
    "image": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/ac/39/01/13774_7183.png?c=35ee", 
    }, 
    { 
    "id": 5, 
    "image": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/a7/3a/01/1386e_d5c5.png?c=3f1e", 
    }, 
    { 
    "id": 3, 
    "image": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/1d/3a/01/137e4_e803.png?c=9ab9", 
    }, 
    { 
    "id": 6, 
    "image": "http://devaddons1.socialengineaddons.com/mobiledemotesting/public/system/cf/3a/01/13896_edab.png?c=1e7b", 

    } 

このJsonArrayから今

、私はこれを行うことができますどのように、私はこれを最適化したいidが2

に等しく持っているJsonObjectを削除します]なぜなら、私はforループを使用してそれを行うことができますが、私はforループを使用してそれをしたくありません。 他の方法がありますか?もし誰かがこれについて知っていれば私を助けてください。

ありがとうございました。この方法を使用して

+0

どのjsonライブラリをお使いですか? –

答えて

0
JSONArray jsonArray = new JSONArray(string); 
int len = jsonArray.length(); 
int position = -1; 
int wantedID = 2; 
for (int i=0;i<len;i++){ 
    JSONObject jsonObj = jsonArray.getJSONObject(i); 
    int id = Integer.parseInt(jsonObj.get("id")); 
    if (id == wantedID) { 
     position = i; 
     break; 
    } 
    } 
JSONArray jsonArray_out = jsonArray; 
     if (position >= 0) { 
//for 19 API use this: jsonArray_out.remove(position); 
//or remove using this code: 
     jsonArray_out = remove(position, jsonArray); 
     } 

     String out = jsonArray_out.toString(); 

あなたはJSONからデータを削除することができます。

https://stackoverflow.com/a/12526047/1979882

public static JSONArray remove(final int idx, final JSONArray from) { 
    final List<JSONObject> objs = asList(from); 
    objs.remove(idx); 

    final JSONArray ja = new JSONArray(); 
    for (final JSONObject obj : objs) { 
     ja.put(obj); 
    } 

    return ja; 
} 

public static List<JSONObject> asList(final JSONArray ja) { 
    final int len = ja.length(); 
    final ArrayList<JSONObject> result = new ArrayList<JSONObject>(len); 
    for (int i = 0; i < len; i++) { 
     final JSONObject obj = ja.optJSONObject(i); 
     if (obj != null) { 
      result.add(obj); 
     } 
    } 
    return result; 
} 

またはこの:

あなたはGsonライブラリから JsonArrayを使用していると仮定すると

https://stackoverflow.com/a/20250881/1979882

+0

私は彼が 'org.json'ではなくcom.google.gsonを使用しているのではないかと心配しています... –

+0

remove()はAPIレベル19を必要とします。 – Prashant

+0

@Prashant、更新済み – Vyacheslav

0

を、を使用できます方法。例えば

:あるいは

int idToRemove = 3; 

JsonElement elementToRemove = null; 
for(JsonElement e : jsonArray) { 
    JsonObject o = e.getAsJsonObject(); 
    if(o.get("id").getAsInt() == idToRemove) { 
     elementToRemove = e; 
     break; 
    } 
} 
if (elementToRemove != null) jsonArray.remove(elementToRemove); 

、明示的Iterator<JsonElement>を使用します。

関連する問題