0

既に持っているものから新しい JSONArray を作成できるようにする必要があります。配列には、id という名前のフィールドがたくさんあり、各行に別のフィールドがあります。それらのリストを取得するにはどうすればよいですか?

例えば。私はこれを取り戻す

[{"id":111,"users":"blob"},
 {"id":111,"users":"blob"},
 {"id":111,"users":"blob"},
 {"id":111,"users":"blob"},
 {"id":111,"users":"blob"},
 {"id":111,"users":"blob"}]

ID だけのリストを取得するにはどうすればよいですか?


  • 編集

この直後に for ループを使用することにしましたが、ありがとう :D

HttpResponse response = httpclient.execute(httpget); //execute the HttpPost request
JSONArray jsa =  projectResponse(response); //get the json response

for(int i = 0; i < jsa.length(); i++){
    JSONObject jso = jsa.getJSONObject(i);
    publishProgress(jso.getString("id"), jso.getString("name"));
}
4

1 に答える 1

1

これを試してください:

JSONArray yourJSONArray;
List<String> tempIDStringCollection = new List<String>();
...

for(int i = 0; i < yourJSONArray.length(); i++){
        String id = yourJSONArray.getJSONObject(i).getString("id");
        tempIDStringCollection.add(id);
}

次に、これを別の JSONArray に転送するには、次を試してください。

JSONArray newArray = new JSONArray(tempIDStringCollection);

作成をスキップするにはList、試してください

JSONArray yourJSONArray;
JSONArray newArray = new JSONArray();
...

for(int i = 0; i < yourJSONArray.length(); i++){
        String id = yourJSONArray.getJSONObject(i).getString("id");
        newArray.put(i, id);
}
于 2012-04-19T02:29:58.513 に答える