android - 如何使用 Volley 在 JSON 中的数组中获取数组中的元素

标签 android json android-volley

我能够从 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/

相关文章:

android - 每个线程只能创建一个 Looper 错误,异步任务

android - 在 View 页面上播放简单的 .wav?

java - 如何从Android上的java webservice获取图像

java - onResponse 方法中的 Volatile 变量没有改变

Android Espresso 2 卡在对话框中

Android gridview 在 fragment 适配器构造函数中丢失

json - 防止 JSON 响应上的 Apache 自定义错误页面

c# - 适用于 Android 的 Xamarin C# : creating a json string?

android - Android Volley 死了吗?

android - 基本网络.performRequest : Unexpected response code 301