[英]Parsing JSON with Jackson return null
我正在使用API在Java上訓練自己。 我的練習主題是詢問Google圖書API,以獲取有關圖書的一些信息。 我使用Jersey作為客戶端,使用Jackson來解析JSON。
我的問題是,當我運行JerseyVolumeGet()
,響應是這樣的:
Volume{title='null', numberOfPages=null, author='null'}
為什么呢 我的錯誤在哪里? 我懷疑解析JSON時出錯,但是看不到確切位置。
這是我的getClass(搜索網址帶有條形碼,對我來說這不是問題)
public class JerseyVolumeGet {
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client
.resource("https://www.googleapis.com/books/v1/volumes?q=1984");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Noob you Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
// read from file, convert it to user class
ObjectMapper mapper = new ObjectMapper();
Volume volume = mapper.readValue(output, Volume.class);
// display to console
System.out.println(volume);
} catch (Exception e) {
e.printStackTrace();
}
}
}
這是我的查詢的JSON結果:
{
"kind": "books#volumes",
"totalItems": 641,
"items": [
{
"kind": "books#volume",
"id": "RY8yQWeDVFYC",
"etag": "Lf0P50PVz9c",
"selfLink": "https://www.googleapis.com/books/v1/volumes/RY8yQWeDVFYC",
"volumeInfo": {
"title": "(1984).",
"subtitle": "",
"authors": [
"Guy Serbat",
"Jean Taillardat",
"Gilbert Lazard"
],
"publisher": "Peeters Publishers",
"publishedDate": "1984-01-01",
"description": "(Peeters 1984)",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "2904685030"
},
{
"type": "ISBN_13",
"identifier": "9782904685033"
}
],
"readingModes": {
"text": false,
"image": true
},
"pageCount": 280,
"printType": "BOOK",
"categories": [
"Language Arts & Disciplines"
],
"contentVersion": "1.1.1.0.preview.1",
"imageLinks": {
"smallThumbnail": "http://bks9.books.google.fr/books?id=RY8yQWeDVFYC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://bks9.books.google.fr/books?id=RY8yQWeDVFYC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "fr",
"previewLink": "http://books.google.fr/books?id=RY8yQWeDVFYC&printsec=frontcover&dq=1984&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.fr/books?id=RY8yQWeDVFYC&dq=1984&hl=&source=gbs_api",
"canonicalVolumeLink": "http://books.google.fr/books/about/1984.html?hl=&id=RY8yQWeDVFYC"
},
"saleInfo": {
"country": "FR",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "FR",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "http://books.google.fr/books/reader?id=RY8yQWeDVFYC&hl=&printsec=frontcover&output=reader&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
},
"searchInfo": {
"textSnippet": "(Peeters 1984)"
}
},
more items...
}
然后,我有一個Volume.class
像這樣:
@JsonIgnoreProperties(ignoreUnknown=true)
public class Volume {
@JsonProperty("title")
String title;
@JsonProperty("pageCount")
Integer pageCount;
@JsonProperty("authors")
String authors;
getters , setters and toString..
您需要為完整的JSON結構建模,如下所示:
public class BookData {
String kind;
Integer totalItems;
List<Item> items;
}
public class Item {
String kind;
String id;
//...
Volume volumeInfo;
}
然后,您可以使用ObjectMapper
來讀取BookData
:
BookData bookData = new ObjectMapper().readValue(output, BookData.class);
並從BookData
每個Item
中提取Volume
信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.