如何解析AS中的JSON数据

时间:2018-02-15 08:38:39

标签: android json android-volley jsonresponse

问题:
我正在以JSON格式从Web服务获取数据。我正在尝试解析“引用”和“作者”,但我无法做到。

以下是数据

{
"success": {
    "total": 1
},
"contents": {
    "quotes": [
        {
            "quote": "You are very powerful, provided you know how powerful you are.",

            "author": "Yogi Bhajan",

            "category": "inspire",
            "date": "2018-02-15",

            "title": "Inspiring Quote of the day"

        }
    ],
    "copyright": "2017-19 theysaidso.com"
  }
}

我的Java代码是:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("quotes");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject getQuote = jsonArray.getJSONObject(i);

                            String quoteOfTheDay = getQuote.getString("quote");

                            String author = getQuote.getString("author");

                            quotesView.append(quoteOfTheDay + author + "\n\n");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

我确信在Java类中解析的逻辑是不正确的 我应该使用哪种逻辑来纠正这个问题?

3 个答案:

答案 0 :(得分:3)

首先,在提取数组JSONObject之前,您必须先获取contents quotes

try {
    JSONObject obj = response.getJSONObject("contents"); //first get the JSONObject contents
    JSONArray jsonArray = obj.getJSONArray("quotes");

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject getQuote = jsonArray.getJSONObject(i);

        String quoteOfTheDay = getQuote.getString("quote");

        String author = getQuote.getString("author");

        quotesView.append(quoteOfTheDay + author + "\n\n");
    }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 1 :(得分:1)

您错过了内容 JSONObject。

复制以下代码:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject contentsObject=response.getJSONObject("contents");
                    JSONArray jsonArray = contentsObject.getJSONArray("quotes");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject getQuote = jsonArray.getJSONObject(i);

                        String quoteOfTheDay = getQuote.getString("quote");

                        String author = getQuote.getString("author");

                        quotesView.append(quoteOfTheDay + author + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});

答案 2 :(得分:1)

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                      JSONObject contents = 
                     response.getJsonObject("contents");
                try {
              JSONArray quotes    = contents.getJSONArray("quotes");
           } catch (JSONException e) {
        e.printStackTrace();
       }
                    for (int i = 0; i < quotes.length(); i++) {
                        JSONObject getQuote = jsonArray.getJSONObject(i);

                        String quoteOfTheDay = getQuote.getString("quote");

                        String author = getQuote.getString("author");

                        quotesView.append(quoteOfTheDay + author + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});

试试这个......