使用Android Volley库解析JSON数组中的JSON对象 [英] Parse JSON objects within JSON array with android volley library

查看:119
本文介绍了使用Android Volley库解析JSON数组中的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自服务器的json响应,附在下面.我想用排球在android中解析此响应.如何解析数组中的对象.

I have a json response from server, attached below. I want to parse this response with volley in android. How do I parse the object(s) within the array.


  {
  "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"
    },

我想访问第一个对象,然后访问下一个对象,以及之后的下一个对象.感激.

I want to access the first object, and the next, and the next after that. Appreciate.

推荐答案

这应该显示标题列表

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 Volley库解析JSON数组中的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆