0

我有一个类似这样的 JSON 响应:

{
  "id_list":["123", "456", "789"],
  ...
}

我想知道如果我想使用 JSONObject 来读取这样一个 id 列表并返回一个List<String>id 例如应该怎么做。我没有看到 JSONObject 中有任何方法可以做这样的事情(参考: http: //www.json.org/javadoc/org/json/JSONObject.html)。最可能的可能是JSONArray,但我不知道我是否使用JSONArray并将列表中的每个值都转换为JSONObject,我如何在没有键的情况下读取它们。

谢谢

4

1 回答 1

2

您可以遍历 JSONArray 并将每个值存储到列表中,然后返回。

JSONObject jo = new JSONObject(jsonString); //
JSONArray ja = jo.getJSONArray("id_list"); // get the JSONArray
List<String> keys = new ArrayList<>();

for(int i=0;i<ja.length();i++){
    keys.add(ja.getString(i)); // iterate the JSONArray and extract the keys
}

return keys; // return the list
于 2013-11-08T08:06:56.553 回答