android - 使用 android volley 库解析 JSON 数组中的 JSON 对象

标签 android json android-volley

我有一个来自服务器的 json 响应,附在下面。我想在 android 中用 volley 解析这个响应。如何解析数组中的对象。

  {
  "status": "ok",
  "source": "techcrunch",
  "sortBy": "top",
  "articles": [
    {
      "author": "Ingrid Lunden, Fitz Tepper",
      "title": "Confirmed: AT&T is buying Time Warner for $85.4B in cash and shares",
      "description": "After days of speculation, the deal is now official: AT&T is acquiring Time Warner for $85 billion in a mix of cash and shares, paving the way for..",
      "url": "http://social.techcrunch.com/2016/10/22/confirmed-att-is-buying-time-warner-for-85-4b-in-cash-and-shares/",
      "urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/946_432_newsroom_release_tw.jpg?w=764&h=400&crop=1",
      "publishedAt": "2016-10-23T00:02:34Z"
    },

我想访问第一个对象,然后是下一个对象,然后是下一个对象。欣赏。

最佳答案

这应该显示标题列表

JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            JSONArray jsonArray = null;
            try {
                jsonArray = response.getJSONArray("articles");
                for(int i=0; i<jsonArray.length(); i++){
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        Log.d(TAG, jsonObject.getString("title"));
                    }
                } catch (JSONException e) {
                        e.printStackTrace();
                }                       }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error: " + error.getMessage());
        }
   });

关于android - 使用 android volley 库解析 JSON 数组中的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40198435/

相关文章:

android - MapView 上的 java.util.ConcurrentModificationException

java - Android 中的 `Unknown` ( `Other` ) 内存泄漏?

java - 将多个 SSL 证书固定添加到 Android KeyStore 不起作用。 (来自资源文件)

java - 如何在 JSP 页面中显示 Pretty Print JSON 字符串

python - 替换花括号中的逗号

javascript - 为什么 json_encode 返回数组而不是对象?

android - 基本网络.performRequest : Unexpected response code 429 (android)

android - 当 Volley Request 被取消时

Android ListView Onclick Intent 数据延迟(工作在第二次点击)

java - 令人困惑的java : a method calls another method which returns something