我有一个来自服务器的 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/