异常一、
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. 用高德地图API 通过详细地址获得经纬度

    http://cloud.sinyway.com/Service/amap.html http://restapi.amap.com/v3/geocode/geo?key=xxxxxxxxxxxxxx ...

  2. php的ob_flush和flush(转)

    php.ini中 output_buffering = off 关闭php的缓存 implicit_flush = Off php不会立即输出到浏览器.如果是ON,相当于每次ECHO 立刻执行一个FL ...

  3. bash if 表达式

    .bash把[[ $a -lt $b ]]看作一个单独的元素,并且返回一个退出码.退出码0为真,非零为假 例如: a= b=c [[ $a -lt $b ]] echo $? # a小于b为真 [[ ...

  4. spring core源码解读之ASM4用户手册翻译之一asm简介

    第一章:ASM介绍 1.1 ASM动机: 程序的分析,生成,转换技术可以应用到许多场景: 1.程序分析,从简单的语法解析到完整的语义分析,可以应用在程序中找到潜在的bug,发现无用的代码,工程代码的逆 ...

  5. ASI进行POST网络请求

    第一步,导入文件 //ASI进行GET数据请求 需要导入的头文件是 ASIHTTPRequest.h //ASI进行POST数据请求 需要导入的头文件是 ASIFormDataRequest.h // ...

  6. 使用C#WebClient类访问(上传/下载/删除/列出文件目录)由IIS搭建的http文件服务器

    前言 为什么要写这边博文呢?其实,就是使用C#WebClient类访问由IIS搭建的http文件服务器的问题花了我足足两天的时间,因此,有必要写下自己所学到的,同时,也能让广大的博友学习学习一下. 本 ...

  7. linux中的网络通信指令 分类: 学习笔记 linux ubuntu 2015-07-06 16:02 134人阅读 评论(0) 收藏

    1.write write命令通信是一对一的通信,即两个人之间的通信,如上图. 效果图 用法:write <用户名> 2.wall wall指令可将信息发送给每位同意接收公众信息的终端机用 ...

  8. Linux Shell删除某一个目录下的所有文件夹(保留其他文件)

    #!/bin/bash direc=$(pwd) for dir2del in $direc/* ; do if [ -d $dir2del ]; then rm -rf $dir2del fi do ...

  9. apache源码编译安装详解

    查看是否安装 rpm -qa httpd        如果已安装,则卸载:rpm -e 卸载  --nodeps 不考虑意外        下载 wget http://mirrors.sohu.c ...

  10. JSTL时间比较,jstl日期比较,jsp比较时间

    >>>>>>>>>>>>>>>>>>>>>>>>> ...