【问题标题】:JSONArray list another array from result?JSONArray 从结果中列出另一个数组?
【发布时间】:2012-04-19 02:15:01
【问题描述】:

我需要能够从我已有的 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"));
}

【问题讨论】:

标签: android json


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 2017-04-28
    • 2018-05-15
    • 2015-08-28
    • 1970-01-01
    • 2022-01-07
    • 2018-11-09
    • 2013-12-16
    • 2016-01-11
    • 1970-01-01
    相关资源
    最近更新 更多