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