我能够从 JSON 页面获取一些信息,但是我很难在数组中获取元素。
到目前为止我的代码:
@Override
public void onResponse(JSONObject response) {
try {
String status = response.getString("status");
if (status.equals("ok")) {
JSONArray jsonArray = response.getJSONArray("articles");
JSONObject jsonObject = jsonArray.getJSONObject(0);
titleTextView.setText(jsonObject.getString("title"));
}
} catch (JSONException e) {
e.printStackTrace();
}
我收到 JSON 的页面是:https://newsapi.org/v2/top-headlines?q=trump&language=en&apiKey=954cfe1dd4db4f8fb19e425c12db324e
到目前为止,我的代码获取了我的第一篇文章的信息,除了来自名为“source”的数组之外
我正在寻找名为“name”的名为“source”的数组中的信息
最佳答案
"source"
不是 JSONArray
它的 JSONObject
从 source
对象获取 name
试试这个
if (status.equals("ok")) {
JSONArray jsonArray = response.getJSONArray("articles");
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONObject sourceObject= jsonObject.getJSONObject("source");
titleTextView.setText(sourceObject.getString("name"));
}
关于android - 如何使用 Volley 在 JSON 中的数组中获取数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724880/