jackson学习----解析豆瓣的图书信息

时间:2022-11-06 14:25:32
异常一、
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.douban.book.Book$Rating]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@1cbfe9d; line: 1, column: 12] (through reference chain: com.douban.book.Book["rating"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:740)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at com.util.HttpIO.doPost(HttpIO.java:33)
at com.douban.book.GetBookInfo.main(GetBookInfo.java:11)

在定义Rating内部类的时候没有使用static修饰

异常二、

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "publisher" (Class com.douban.book.Book), not marked as ignorable
at [Source: java.io.StringReader@1b8f864; line: 1, column: 852] (through reference chain: com.douban.book.Book["publisher"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:649)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:635)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1355)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:717)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at com.util.HttpIO.doPost(HttpIO.java:33)
at com.douban.book.GetBookInfo.main(GetBookInfo.java:11)

publisher变量没有定义或者定义成private但是没有get set方法

异常三、

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: java.io.StringReader@1112783; line: 1, column: 75] (through reference chain: com.douban.book.Book["author"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:44)
at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:13)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2723)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
at com.util.HttpIO.doPost(HttpIO.java:33)
at com.douban.book.GetBookInfo.main(GetBookInfo.java:11)

author应该是List<String>类型,定义成了String类型

---------------------------------分割线-------------------------------------------------

重要的几个类:

解析json方向

//解析器

org.codehaus.jackson.map.ObjectMapper;

//解析配置

org.codehaus.jackson.map.DeserializationConfig;

objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

objectMapper.setDeserializationConfig(objectMapper.getDeserializationConfig().without(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES))

关于上面这个属性

关于jackson的DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES

-------------------------------------------------------------------------------------------------

注意使用Jackson解析json的一些方法和原则:

{}全部需要定义成类

[] 全部需要定义成List<>

内部类需要定义成static

所有的key都是value变量名对应

还有就是一些方法需要学习

http://itindex.net/detail/51074-java-%E5%88%A9%E7%94%A8-jackson

http://developers.douban.com/wiki/?title=guide

com.util.HttpIO类

package com.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper; import com.douban.book.Book; public class HttpIO
{
public static void doPost(final String url, final String body)
{ HttpClient httpclient = new DefaultHttpClient();
ObjectMapper mapper = new ObjectMapper(); HttpPost post = new HttpPost(url);
try
{
HttpResponse result = httpclient.execute(post); if (result.getStatusLine().getStatusCode() == 200)
{
String str = EntityUtils.toString(result.getEntity());
System.out.println(str); Book book = mapper.readValue(str, Book.class);
System.out.println(book);
}
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

  

package com.douban.book;

import com.util.HttpIO;

//http://api.douban.com/v2/book/1220562
public class GetBookInfo
{
public static void main(String[] args)
{
HttpIO.doPost("http://api.douban.com/v2/book/1220562",null);
}
}

  

package com.douban.book;

import java.util.List;

public class Book
{
private String publisher; public String id; private String alt; private Rating rating; private List<String> author; private String alt_title; private Images images; private String title; private String mobile_link; private String summary; private Attrs attrs; private List<Tags> tags; private String subtitle; private String pubdate; private String isbn10; private String isbn13; private String url; private String author_intro; private String price; private String origin_title; private String binding; private String catalog; private String pages; private String image; private List<String> translator; public List<String> getTranslator()
{
return translator;
} public void setTranslator(List<String> translator)
{
this.translator = translator;
} public String getImage()
{
return image;
} public void setImage(String image)
{
this.image = image;
} public Images getImages()
{
return images;
} public void setImages(Images images)
{
this.images = images;
} public String getBinding()
{
return binding;
} public void setBinding(String binding)
{
this.binding = binding;
} public String getCatalog()
{
return catalog;
} public void setCatalog(String catalog)
{
this.catalog = catalog;
} public String getPages()
{
return pages;
} public void setPages(String pages)
{
this.pages = pages;
} static class Images
{
private String small; private String large; private String medium; public String getSmall()
{
return small;
} public void setSmall(String small)
{
this.small = small;
} public String getLarge()
{
return large;
} public void setLarge(String large)
{
this.large = large;
} public String getMedium()
{
return medium;
} public void setMedium(String medium)
{
this.medium = medium;
} } public String getOrigin_title()
{
return origin_title;
} public void setOrigin_title(String origin_title)
{
this.origin_title = origin_title;
} public Book()
{ } public Book(String str)
{ } public String getPubdate()
{
return pubdate;
} public void setPubdate(String pubdate)
{
this.pubdate = pubdate;
} public String getIsbn10()
{
return isbn10;
} public void setIsbn10(String isbn10)
{
this.isbn10 = isbn10;
} public String getIsbn13()
{
return isbn13;
} public void setIsbn13(String isbn13)
{
this.isbn13 = isbn13;
} public String getUrl()
{
return url;
} public void setUrl(String url)
{
this.url = url;
} public String getAuthor_intro()
{
return author_intro;
} public void setAuthor_intro(String author_intro)
{
this.author_intro = author_intro;
} public String getPrice()
{
return price;
} public void setPrice(String price)
{
this.price = price;
} public String getSubtitle()
{
return subtitle;
} public void setSubtitle(String subtitle)
{
this.subtitle = subtitle;
} public String getAlt()
{
return alt;
} public void setAlt(String alt)
{
this.alt = alt;
} public Rating getRating()
{
return rating;
} public void setRating(Rating rating)
{
this.rating = rating;
} public List<String> getAuthor()
{
return author;
} public void setAuthor(List<String> author)
{
this.author = author;
} public String getAlt_title()
{
return alt_title;
} public void setAlt_title(String alt_title)
{
this.alt_title = alt_title;
} public String getTitle()
{
return title;
} public void setTitle(String title)
{
this.title = title;
} public String getMobile_link()
{
return mobile_link;
} public void setMobile_link(String mobile_link)
{
this.mobile_link = mobile_link;
} public String getSummary()
{
return summary;
} public void setSummary(String summary)
{
this.summary = summary;
} public Attrs getAttrs()
{
return attrs;
} public void setAttrs(Attrs attrs)
{
this.attrs = attrs;
} public List<Tags> getTags()
{
return tags;
} public void setTags(List<Tags> tags)
{
this.tags = tags;
} static private class Tags
{
private String count; private String name; private String title; public String getTitle()
{
return title;
} public void setTitle(String title)
{
this.title = title;
} public String getCount()
{
return count;
} public void setCount(String count)
{
this.count = count;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
}
} static private class Attrs
{
private List<String> publiser; private List<String> pubdate; private List<String> author; private List<String> price; private List<String> title; private List<String> binding; private List<String> translator; private List<String> pages; public List<String> getPubliser()
{
return publiser;
} public void setPubliser(List<String> publiser)
{
this.publiser = publiser;
} public List<String> getPubdate()
{
return pubdate;
} public void setPubdate(List<String> pubdate)
{
this.pubdate = pubdate;
} public List<String> getAuthor()
{
return author;
} public void setAuthor(List<String> author)
{
this.author = author;
} public List<String> getPrice()
{
return price;
} public void setPrice(List<String> price)
{
this.price = price;
} public List<String> getTitle()
{
return title;
} public void setTitle(List<String> title)
{
this.title = title;
} public List<String> getBinding()
{
return binding;
} public void setBinding(List<String> binding)
{
this.binding = binding;
} public List<String> getTranslator()
{
return translator;
} public void setTranslator(List<String> translator)
{
this.translator = translator;
} public List<String> getPages()
{
return pages;
} public void setPages(List<String> pages)
{
this.pages = pages;
}
} static class Rating
{
private int max; private String average; private int numRaters; private int min; public int getMax()
{
return max;
} public void setMax(int max)
{
this.max = max;
} public String getAverage()
{
return average;
} public void setAverage(String average)
{
this.average = average;
} public int getNumRaters()
{
return numRaters;
} public void setNumRaters(int numRaters)
{
this.numRaters = numRaters;
} public int getMin()
{
return min;
} public void setMin(int min)
{
this.min = min;
} } public String getPublisher()
{
return publisher;
} public void setPublisher(String publisher)
{
this.publisher = publisher;
} }
{jackson学习----解析豆瓣的图书信息
"rating": {jackson学习----解析豆瓣的图书信息
"max": 10,
"numRaters": 345,
"average": "7.0",
"min": 0
},
"subtitle": "",
"author": [jackson学习----解析豆瓣的图书信息
"[日] 片山恭一"
],
"pubdate": "2005-1",
"tags": [jackson学习----解析豆瓣的图书信息
{jackson学习----解析豆瓣的图书信息
"count": 133,
"name": "片山恭一",
"title": "片山恭一"
},
{jackson学习----解析豆瓣的图书信息
"count": 62,
"name": "日本",
"title": "日本"
},
{jackson学习----解析豆瓣的图书信息
"count": 60,
"name": "日本文学",
"title": "日本文学"
},
{jackson学习----解析豆瓣的图书信息
"count": 38,
"name": "小说",
"title": "小说"
},
{jackson学习----解析豆瓣的图书信息
"count": 32,
"name": "满月之夜白鲸现",
"title": "满月之夜白鲸现"
},
{jackson学习----解析豆瓣的图书信息
"count": 15,
"name": "爱情",
"title": "爱情"
},
{jackson学习----解析豆瓣的图书信息
"count": 8,
"name": "純愛",
"title": "純愛"
},
{jackson学习----解析豆瓣的图书信息
"count": 8,
"name": "外国文学",
"title": "外国文学"
}
],
"origin_title": "",
"image": "http://img3.douban.com/mpic/s1747553.jpg",
"binding": "平装",
"translator": [jackson学习----解析豆瓣的图书信息
"豫人"
],
"catalog": "
",
"pages": "180",
"images": {jackson学习----解析豆瓣的图书信息
"small": "http://img3.douban.com/spic/s1747553.jpg",
"large": "http://img3.douban.com/lpic/s1747553.jpg",
"medium": "http://img3.douban.com/mpic/s1747553.jpg"
},
"alt": "http://book.douban.com/subject/1220562/",
"id": "1220562",
"publisher": "青岛出版社",
"isbn10": "7543632608",
"isbn13": "9787543632608",
"title": "满月之夜白鲸现",
"url": "http://api.douban.com/v2/book/1220562",
"alt_title": "",
"author_intro": "",
"summary": "那一年,是听莫扎特、钓鲈鱼和家庭破裂的一年。说到家庭破裂,母亲怪自己当初没有找到好男人,父亲则认为当时是被狐狸精迷住了眼,失常的是母亲,但出问题的是父亲……。",
"price": "15.00元"
}

对于Book类当中的Bean变量 可以定义成private的 然后赋予get set方法,也可以直接定义成public的