异常一、
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;
} }
{
"rating": {
"max": 10,
"numRaters": 345,
"average": "7.0",
"min": 0
},
"subtitle": "",
"author": [
"[日] 片山恭一"
],
"pubdate": "2005-1",
"tags": [
{
"count": 133,
"name": "片山恭一",
"title": "片山恭一"
},
{
"count": 62,
"name": "日本",
"title": "日本"
},
{
"count": 60,
"name": "日本文学",
"title": "日本文学"
},
{
"count": 38,
"name": "小说",
"title": "小说"
},
{
"count": 32,
"name": "满月之夜白鲸现",
"title": "满月之夜白鲸现"
},
{
"count": 15,
"name": "爱情",
"title": "爱情"
},
{
"count": 8,
"name": "純愛",
"title": "純愛"
},
{
"count": 8,
"name": "外国文学",
"title": "外国文学"
}
],
"origin_title": "",
"image": "http://img3.douban.com/mpic/s1747553.jpg",
"binding": "平装",
"translator": [
"豫人"
],
"catalog": "
",
"pages": "180",
"images": {
"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的

jackson学习----解析豆瓣的图书信息的更多相关文章

  1. Python爬虫(一)——豆瓣下图书信息

    爬虫目的: 随着近年互联网的发展,网络上的信息飞速数量增长.在庞大的数据面前想要获得期望的信息往往如同大海捞针.通过合理的筛选,在百万甚至数亿计的数据中找到所需信息,无疑有着非常大的意义. 在豆瓣网下 ...

  2. C# 通过豆瓣网络编程API获取图书信息

    这篇文章主要是关于如何通过豆瓣API获取信息的书籍,起初,我看到了原来的想法的内容是"C# 网络编程之网页简单下载实现"中通过HttpWebResponse类下载源代码,再通过正則 ...

  3. 003.[python学习] 简单抓取豆瓣网电影信息程序

    声明:本程序仅用于学习爬网页数据,不可用于其它用途. 本程序仍有很多不足之处,请读者不吝赐教. 依赖:本程序依赖BeautifulSoup4和lxml,如需正确运行,请先安装.下面是代码: #!/us ...

  4. 爬取豆瓣网图书TOP250的信息

    爬取豆瓣网图书TOP250的信息,需要爬取的信息包括:书名.书本的链接.作者.出版社和出版时间.书本的价格.评分和评价,并把爬取到的数据存储到本地文件中. 参考网址:https://book.doub ...

  5. Book2Notion:将豆瓣图书信息同步到Notion的Chrome插件

    背景 前几天写了一个python脚本从豆瓣爬数据然后保存到Notion,被身边同学吐槽使用起来太麻烦,而且也不是所有人都会Python(原话是充满了码农版"何不食肉糜").正好最近 ...

  6. @1-4使用Xpath解析豆瓣短评

    使用Xpath解析豆瓣短评 Python爬虫(入门+进阶)     DC学院 本节课程主要介绍解析神器Xpath是什么.Xpath如何安装及使用,以及使用实际的例子讲解Xpath如何解析豆瓣短评的网页 ...

  7. jackson学习之一:基本信息

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. jackson学习之二:jackson-core

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. jackson学习之九:springboot整合(配置文件)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 系列文章汇总 jackson学习之一:基本信息 jac ...

随机推荐

  1. 怎样在win7下装ubuntu(硬盘版安装)

    1)首先还是分区,在计算机上右键--管理--磁盘管理 装Ubuntu分配的硬盘大小最好是(20G以上)不要太小,这里请注意,ubuntu和windows文件系统全然不同,所以我们划好要给ubuntu的 ...

  2. MFC 一个类訪问还有一个类成员对象的成员变量值

    作者:卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/35263857 MFC中一个类要訪问另外一个类的的对象的成员变量值,这就须要获得 ...

  3. Android菜单详解(四)——使用上下文菜单ContextMenu

    之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...

  4. 关于tomcat的思考

    下载文件两种方式:绿色版的.安装版的(找到jre的环境变量.配置或修改端口8080→8070) 启动完tomcat之后: 既可以虚拟目录打开(如http://localhost:8070/mldn/) ...

  5. 【转】谈Objective-c block的实现

    本文转自http://blog.devtang.com/blog/2013/07/28/a-look-inside-blocks/,如有侵权,请联系我删除 前言 这里有关于block的5道测试题,建议 ...

  6. android开发之Bundle使用

    android开发中,我们经常需要在两个activity之间传递数据,最常用的莫过于使用intent.putXXX(),可是很多时候我们也会这样: Bundle bundle = new Bundle ...

  7. 购买SSD固态硬盘须当心,你知道什么是SLC、 MLC、TLC闪存芯片颗粒吗?

    固态硬盘凭借其存取速率超快等自身优势,被越来越多的电脑爱好者所青睐,并迅速普及到了广大用户的电脑中,因为固态硬盘与传统机械硬盘相比,确实在运行效率等方面有了质的提升,但是亦是美网络小编要提醒大家的是, ...

  8. Linux删除文件Argument list too long问题的解决方案

    方法一:使用find find . -name 文件 | xargs rm -f 但文件数量过多,find命令也会出现问题: -bash: /bin/find: Argument list too l ...

  9. Java代码操作HDFS(在/user/root/下面創建目錄)

    1.创建HDFS目录并打成jar包 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.h ...

  10. Big Data應用:以"玩家意見"之數據分析來探討何謂"健康型線上遊戲"(上)

    首先,所有資料都可以從網路上找到,只是我做了一些分析與整理而已.純粹分享心得~~ 最近再做研究的時候我跟我的同事K先生在某次偶然的討論中發現了一件有趣的事情. [疑~~~~~~~新楓之谷的玩家人氣指數 ...