0

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

这是数据

{
"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 类中解析的逻辑是不正确的。
我应该使用哪种逻辑来纠正这个问题?

4

3 回答 3

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();
}
于 2018-02-15T08:42:47.993 回答
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();
    }
});
于 2018-02-15T08:44:35.497 回答
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();
    }
});

尝试这个...

于 2018-02-15T08:44:43.357 回答