【发布时间】:2016-09-13 19:19:41
【问题描述】:
我正在创建一个应用程序,它使用 Google API 获取有关 google Books 的详细信息。
我想从以下 JSON 链接中检索有关作者和书名的数据。
https://www.googleapis.com/books/v1/volumes?q=android
我解决问题的方法:
private Book extractFeatureFromJson(String bookJSON) {
try {
JSONObject baseJsonResponse = new JSONObject(bookJSON);
JSONArray items = baseJsonResponse.getJSONArray("items");
// If there are results in the features array
for(int i=0;i<10;i++)
{
JSONObject firstFeature = items.getJSONObject(i);
JSONObject volumeinfo=firstFeature.getJSONObject("volumeinfo");
String title=volumeinfo.getString("title");
JSONArray author= volumeinfo.getJSONArray("authors");
for(int j=0;j<author.length();j++)
{
JSONObject authorFeature=author.getJSONObject(j);
}
// Create a new {@link Event} object
return new Book(title,author);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the earthquake JSON results", e);
}
return null;
}
我对路径的方法是错误的,我无法弄清楚错误在哪里。
【问题讨论】:
-
你能把 JSON 响应放上去吗?
-
author.getJSONObject(j);是什么让你认为authors的内容是对象?
标签: android json google-api