我必须解析下面的代码,但我不知道如何从中获取值
{"response":{"status":"ok","userTier":"developer","total":18296,"startIndex":1,"pageSize":10,"currentPage":1,"pages":1830,"orderBy":"newest","results":[{"id":"community/2016/dec/10/your-pictures-share-your-photos-on-the-theme-of-gift","type":"article","sectionId":"community","sectionName":"Community","webPublicationDate":"2016-12-10T22:30:10Z","webTitle":"Your pictures: share your photos on the theme of 'gift'","webUrl":"https://www.theguardian.com/community/2016/dec/10/your-pictures-share-your-photos-on-the-theme-of-gift","apiUrl":"https://content.guardianapis.com/community/2016/dec/10/your-pictures-share-your-photos-on-the-theme-of-gift","isHosted":false},{"id":"media/2016/dec/10/aa-gill-obituary","type":"article","sectionId":"media","sectionName":"Media","webPublicationDate":"2016-12-10T18:12:40Z","webTitle":"AA Gill obituary","webUrl":"https://www.theguardian.com/media/2016/dec/10/aa-gill-obituary","apiUrl":"https://content.guardianapis.com/media/2016/dec/10/aa-gill-obituary","isHosted":false},{"id":"society/2016/dec/10/acutely-ill-children-waiting-nine-hours-for-beds-nhs-paediatricians","type":"article","sectionId":"society","sectionName":"Society","webPublicationDate":"2016-12-10T15:56:00Z","webTitle":"Acutely ill children waiting nine hours for beds in intensive care","webUrl":"https://www.theguardian.com/society/2016/dec/10/acutely-ill-children-waiting-nine-hours-for-beds-nhs-paediatricians","apiUrl":"https://content.guardianapis.com/society/2016/dec/10/acutely-ill-children-waiting-nine-hours-for-beds-nhs-paediatricians","isHosted":false},{"id":"media/2016/dec/10/aa-gill-dies-weeks-after-revealing-he-had-cancer-in-restaurant-review-sunday-times","type":"article","sectionId":"media","sectionName":"Media","webPublicationDate":"2016-12-10T15:52:01Z","webTitle":"AA Gill dies weeks after revealing he had cancer in restaurant review","webUrl":"https://www.theguardian.com/media/2016/dec/10/aa-gill-dies-weeks-after-revealing-he-had-cancer-in-restaurant-review-sunday-times","apiUrl":"https://content.guardianapis.com/media/2016/dec/10/aa-gill-dies-weeks-after-revealing-he-had-cancer-in-restaurant-review-sunday-times","isHosted":false},{"id":"lifeandstyle/2016/dec/10/medical-marijuana-research-new-treatments","type":"article","sectionId":"lifeandstyle","sectionName":"Life and style","webPublicationDate":"2016-12-10T15:00:00Z","webTitle":"New medical marijuana research could greenlight more uses in treatment","webUrl":"https://www.theguardian.com/lifeandstyle/2016/dec/10/medical-marijuana-research-new-treatments","apiUrl":"https://content.guardianapis.com/lifeandstyle/2016/dec/10/medical-marijuana-research-new-treatments","isHosted":false},{"id":"lifeandstyle/2016/dec/10/the-teen-power-list","type":"article","sectionId":"lifeandstyle","sectionName":"Life and style","webPublicationDate":"2016-12-10T09:00:10Z","webTitle":"‘It's not about your age, it's about your ideas’: the teen power list","webUrl":"https://www.theguardian.com/lifeandstyle/2016/dec/10/the-teen-power-list","apiUrl":"https://content.guardianapis.com/lifeandstyle/2016/dec/10/the-teen-power-list","isHosted":false},{"id":"football/2016/dec/09/chelsea-david-luiz-struggling-sergio-aguero-challenge","type":"article","sectionId":"football","sectionName":"Football","webPublicationDate":"2016-12-09T22:30:41Z","webTitle":"Chelsea’s David Luiz struggling from effects of Sergio Agüero lunge","webUrl":"https://www.theguardian.com/football/2016/dec/09/chelsea-david-luiz-struggling-sergio-aguero-challenge","apiUrl":"https://content.guardianapis.com/football/2016/dec/09/chelsea-david-luiz-struggling-sergio-aguero-challenge","isHosted":false},{"id":"sport/2016/dec/09/amir-khan-wife-faryal-makhdoom-accuses-in-laws-bullying-abuse","type":"article","sectionId":"sport","sectionName":"Sport","webPublicationDate":"2016-12-09T12:39:01Z","webTitle":"Amir Khan’s wife accuses in-laws of bullying and abuse","webUrl":"https://www.theguardian.com/sport/2016/dec/09/amir-khan-wife-faryal-makhdoom-accuses-in-laws-bullying-abuse","apiUrl":"https://content.guardianapis.com/sport/2016/dec/09/amir-khan-wife-faryal-makhdoom-accuses-in-laws-bullying-abuse","isHosted":false},{"id":"world/2016/dec/09/syria-refugees-canada-government-stipend-justin-trudeau","type":"article","sectionId":"world","sectionName":"World news","webPublicationDate":"2016-12-09T11:45:28Z","webTitle":"Syrian refugees in Canada lose support one year on: 'How are we going to live?'","webUrl":"https://www.theguardian.com/world/2016/dec/09/syria-refugees-canada-government-stipend-justin-trudeau","apiUrl":"https://content.guardianapis.com/world/2016/dec/09/syria-refugees-canada-government-stipend-justin-trudeau","isHosted":false},{"id":"society/2016/dec/09/self-harm-children-rises-steeply-england-wales","type":"article","sectionId":"society","sectionName":"Society","webPublicationDate":"2016-12-09T00:01:13Z","webTitle":"Self-harm by children rises steeply in England and Wales","webUrl":"https://www.theguardian.com/society/2016/dec/09/self-harm-children-rises-steeply-england-wales","apiUrl":"https://content.guardianapis.com/society/2016/dec/09/self-harm-children-rises-steeply-england-wales","isHosted":false}]}}
在应用程序中,我使用此代码来抓取它,但它永远不会进入'尝试'
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"https://content.guardianapis.com/search?q=cancer%20new%20treatment&order-by=newest&api-key=test",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("Mpikeeeee");
try {
JSONArray newsItems = response.getJSONArray("response");
for (int i = 0; i < newsItems.length(); i++) {
JSONObject temp = newsItems.getJSONObject(i);
String title = temp.getString("webTitle");
newsFeed.add(new newsItem(title, "", "", "", "", 0));
}
} catch (JSONException e) {
Log.i("myTag4", e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag1", error.toString());
}
});
答案 0 :(得分:1)
检查您的响应,json数组是results
而不是response
。
{
"response": {
"status": "ok",
...
"orderBy": "newest",
"results": [{
"id": "community/2016/dec/10/your-pictures-share-your-photos-on-the-theme-of-gift",
"type": "article",
"sectionId": "community",
"sectionName": "Community",
"webPublicationDate": "2016-12-10T22:30:10Z",
"webTitle": "Your pictures: share your photos on the theme of 'gift'",
"webUrl": "https://www.theguardian.com/community/2016/dec/10/your-pictures-share-your-photos-on-the-theme-of-gift",
"apiUrl": "https://content.guardianapis.com/community/2016/dec/10/your-pictures-share-your-photos-on-the-theme-of-gift",
"isHosted": false
},
{
"id": "media/2016/dec/10/aa-gill-obituary",
"type": "article",
"sectionId": "media",
"sectionName": "Media",
"webPublicationDate": "2016-12-10T18:12:40Z",
"webTitle": "AA Gill obituary",
"webUrl": "https://www.theguardian.com/media/2016/dec/10/aa-gill-obituary",
"apiUrl": "https://content.guardianapis.com/media/2016/dec/10/aa-gill-obituary",
"isHosted": false
}
...
}]
}
}
像这样改变你的实现。
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"https://content.guardianapis.com/search?q=cancer%20new%20treatment&order-by=newest&api-key=test",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("Mpikeeeee");
try {
JSONObject responseNode = response.getJSONObject("response");
JSONArray newsItems = responseNode.getJSONArray("results");
for (int i = 0; i < newsItems.length(); i++) {
JSONObject temp = newsItems.getJSONObject(i);
String title = temp.getString("webTitle");
newsFeed.add(new newsItem(title, "", "", "", "", 0));
}
} catch (JSONException e) {
Log.i("myTag4", e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag1", error.toString());
}
});
希望这有帮助!