我需要能够从我已经拥有的JSONArray中创建一个新的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"));
}
答案 0 :(得分: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);
}