如何使用 GSON 和 Jsoup 从 Google 本书 api 中提取字段
How to extract field from Google books api using GSON and Jsoup
我是新来的,我正在尝试从 ISBN Google API 中提取标题和作者。
代码如下:
try {
Document docKb = Jsoup.connect("https://www.googleapis.com/books/v1/volumes?q=isbn:0735619670").ignoreContentType(true).get();
String json = docKb.body().text();
Gson gson = new Gson();
//new Gson().toJson(new HashMap(map));
Map<String, Object> asMap = gson.fromJson(json, Map.class);
List<Map<String, Object>> items = (List) asMap.get("items");
// Map<String, Object> e = (Map) error.get("error")
for (Map<String, Object> item : items) {
if (item.containsKey("title") && item.containsKey("authors")) {
String title = (String) item.get("title");
System.out.println("if Título: " + title);
} else {
System.out.println("Título: " + item.get("title") + "\n");
System.out.println("Autor: " + item.get("authors"));
}
}
System.out.println("items: "+ items );
}catch(IOException e){
e.printStackTrace();
}
它没有用...标题和作者的值为空,但在列表 'items' 中它已经从 API.
中删除了所有内容
这里发生的是一个简单的 JSON 解析错误。你没有给 gson 正确的 class。简单地说,这个 JSON 不是 Map
。相反,它是一个对象,包含:
String kind;
int totalItems;
Object items;
下面我提供了正确解析此 JSON 所需的完整代码(假设您能够正确获取 JSON 字符串。
class ClassWhatever {
public static void main(String[] args) {
String url = "https://www.googleapis.com/books/v1/volumes?q=isbn:0735619670";
// Assuming that you do in fact have the JSON string...
String json = "the correct json";
Container fullJsonObject = new Gson().fromJson(json, Container.class);
for (Item i : fullJsonObject.items) {
System.out.println(i.volumeInfo.authors[0]);
}
}
private class Container {
String kind;
int totalItems;
Item[] items;
}
private class Item {
String kind;
String id;
String etag;
///blah
VolumeInfo volumeInfo;
String publisher;
///etc.
}
private class VolumeInfo {
String title;
String[] authors;
}
}
输出:
Steve McConnell
Steve McConnell
备注:
您只需添加所需的字段。例如,如果您不需要 String kind
,就不要将它放在 Container
class 中。为了简洁起见,我省略了很多字段,但如果您需要它们,当然会把它们填进去。
此外,我选择使用数组而不是列表。只要您正确格式化代码,它们就可以完全互换。
我是新来的,我正在尝试从 ISBN Google API 中提取标题和作者。
代码如下:
try {
Document docKb = Jsoup.connect("https://www.googleapis.com/books/v1/volumes?q=isbn:0735619670").ignoreContentType(true).get();
String json = docKb.body().text();
Gson gson = new Gson();
//new Gson().toJson(new HashMap(map));
Map<String, Object> asMap = gson.fromJson(json, Map.class);
List<Map<String, Object>> items = (List) asMap.get("items");
// Map<String, Object> e = (Map) error.get("error")
for (Map<String, Object> item : items) {
if (item.containsKey("title") && item.containsKey("authors")) {
String title = (String) item.get("title");
System.out.println("if Título: " + title);
} else {
System.out.println("Título: " + item.get("title") + "\n");
System.out.println("Autor: " + item.get("authors"));
}
}
System.out.println("items: "+ items );
}catch(IOException e){
e.printStackTrace();
}
它没有用...标题和作者的值为空,但在列表 'items' 中它已经从 API.
中删除了所有内容这里发生的是一个简单的 JSON 解析错误。你没有给 gson 正确的 class。简单地说,这个 JSON 不是 Map
。相反,它是一个对象,包含:
String kind;
int totalItems;
Object items;
下面我提供了正确解析此 JSON 所需的完整代码(假设您能够正确获取 JSON 字符串。
class ClassWhatever {
public static void main(String[] args) {
String url = "https://www.googleapis.com/books/v1/volumes?q=isbn:0735619670";
// Assuming that you do in fact have the JSON string...
String json = "the correct json";
Container fullJsonObject = new Gson().fromJson(json, Container.class);
for (Item i : fullJsonObject.items) {
System.out.println(i.volumeInfo.authors[0]);
}
}
private class Container {
String kind;
int totalItems;
Item[] items;
}
private class Item {
String kind;
String id;
String etag;
///blah
VolumeInfo volumeInfo;
String publisher;
///etc.
}
private class VolumeInfo {
String title;
String[] authors;
}
}
输出:
Steve McConnell
Steve McConnell
备注:
您只需添加所需的字段。例如,如果您不需要 String kind
,就不要将它放在 Container
class 中。为了简洁起见,我省略了很多字段,但如果您需要它们,当然会把它们填进去。
此外,我选择使用数组而不是列表。只要您正确格式化代码,它们就可以完全互换。