xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.waegg</groupId> <artifactId>jacksonTest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.7.1</version> </dependency> </dependencies> <packaging>war</packaging> </project>
二、Bean代码
AccountBean :
Java代码
- packagecom.bugyun.pojo;
- publicclassAccountBean{
- privateintid;
- privateStringname;
- privateStringemail;
- privateStringaddress;
- privateBirthdaybirthday;
- //getter、setter
- @Override
- publicStringtoString(){
- returnthis.name+"#"+this.id+"#"+this.address+"#"+this.birthday+"#"+this.email;
- }
- publicintgetId(){
- returnid;
- }
- publicvoidsetId(intid){
- this.id=id;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetEmail(){
- returnemail;
- }
- publicvoidsetEmail(Stringemail){
- this.email=email;
- }
- publicStringgetAddress(){
- returnaddress;
- }
- publicvoidsetAddress(Stringaddress){
- this.address=address;
- }
- publicBirthdaygetBirthday(){
- returnbirthday;
- }
- publicvoidsetBirthday(Birthdaybirthday){
- this.birthday=birthday;
- }
- }
Birthday:
Java代码
- packagecom.bugyun.pojo;
- publicclassBirthday{
- privateStringbirthday;
- publicBirthday(Stringbirthday){
- super();
- this.birthday=birthday;
- }
- //getter、setter
- publicBirthday(){
- }
- publicStringgetBirthday(){
- returnbirthday;
- }
- publicvoidsetBirthday(Stringbirthday){
- this.birthday=birthday;
- }
- @Override
- publicStringtoString(){
- returnthis.birthday;
- }
- }
三、测试代码
Java代码
- packagecom.bugyun.test;
- importjava.io.IOException;
- importjava.io.StringWriter;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.Iterator;
- importjava.util.List;
- importjava.util.Map;
- importjava.util.Set;
- importorg.codehaus.jackson.JsonEncoding;
- importorg.codehaus.jackson.JsonGenerationException;
- importorg.codehaus.jackson.JsonGenerator;
- importorg.codehaus.jackson.JsonParseException;
- importorg.codehaus.jackson.map.JsonMappingException;
- importorg.codehaus.jackson.map.ObjectMapper;
- importorg.junit.After;
- importorg.junit.Before;
- importorg.junit.Test;
- importcom.bugyun.pojo.AccountBean;
- importcom.fasterxml.jackson.dataformat.xml.XmlMapper;
- /**
- *项目名称:jacksonTest
- *类名称:JacksonTest.java
- *类描述:
- *创建人:beyond
- *创建时间:2016年2月24日上午11:24:33
- *修改人:
- *修改时间:
- *修改备注:
- *@version
- */
- publicclassJacksonTest{
- privateJsonGeneratorjsonGenerator=null;
- privateObjectMapperobjectMapper=null;
- privateAccountBeanbean=null;
- @Before
- publicvoidinit(){
- bean=newAccountBean();
- bean.setAddress("js_wuxi");
- bean.setEmail("bugyun@hotmail.com");
- bean.setId(1);
- bean.setName("bugyun");
- objectMapper=newObjectMapper();
- try{
- jsonGenerator=objectMapper.getJsonFactory().createJsonGenerator(System.out,JsonEncoding.UTF8);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- @After
- publicvoiddestory(){
- try{
- if(jsonGenerator!=null){
- jsonGenerator.flush();
- }
- if(!jsonGenerator.isClosed()){
- jsonGenerator.close();
- }
- jsonGenerator=null;
- objectMapper=null;
- bean=null;
- System.gc();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- /**
- *
- *@description:JavaBean(Entity/Model)转换成JSON
- *上面分别利用JsonGenerator的writeObject方法和ObjectMapper的writeValue方法完成对Java对象的转换,二者传递的参数及构造的方式不同;
- *JsonGenerator的创建依赖于ObjectMapper对象。
- *也就是说如果你要使用JsonGenerator来转换JSON,那么你必须创建一个ObjectMapper。
- *但是你用ObjectMapper来转换JSON,则不需要JSONGenerator。objectMapper的writeValue方法可以将一个Java对象转换成JSON。
- *这个方法的参数一,需要提供一个输出流,转换后可以通过这个流来输出转换后的内容。
- *或是提供一个File,将转换后的内容写入到File中。当然,这个参数也可以接收一个JSONGenerator,然后通过JSONGenerator来输出转换后的信息。
- *第二个参数是将要被转换的Java对象。如果用三个参数的方法,那么是一个Config。
- *这个config可以提供一些转换时的规则,过指定的Java对象的某些属性进行过滤或转换等。
- *
- *输出:
- *jsonGenerator{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}
- *ObjectMapper{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}
- *@returnvoid
- *@throws
- *@authorbeyond
- *@data:2016年2月24日上午11:34:53
- */
- @Test
- publicvoidwriteEntityJSON(){
- try{
- System.out.println("jsonGenerator");
- jsonGenerator.writeObject(bean);
- System.out.println("\nObjectMapper");
- objectMapper.writeValue(System.out,bean);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- /**
- *@description:将Map集合转换成Json字符串
- *输出:
- *jsonGenerator{"name":"bugyun","account":{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}}
- *ObjectMapper{"name":"bugyun","account":{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}}
- *@returnvoid
- *@throws
- *@authorbeyond
- *@data:2016年2月24日上午11:41:48
- */
- @Test
- publicvoidwriteMapJSON(){
- try{
- Map<String,Object>map=newHashMap<String,Object>();
- map.put("name",bean.getName());
- map.put("account",bean);
- System.out.println("jsonGenerator");
- jsonGenerator.writeObject(map);
- System.out.println("\nObjectMapper");
- objectMapper.writeValue(System.out,map);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- /**
- *
- *@description:将List集合转换成json
- *外面就是多了个[]中括号;同样Array也可以转换,转换的JSON和上面的结果是一样的
- *输出:
- *jsonGenerator[{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}]
- *ObjectMapper1###[{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}]
- *2###[{"id":1,"name":"bugyun","email":"bugyun@hotmail.com","address":"js_wuxi","birthday":null}]
- *@returnvoid
- *@throws
- *@authorbeyond
- *@data:2016年2月24日上午11:46:41
- */
- @Test
- publicvoidwriteListJSON(){
- try{
- List<AccountBean>list=newArrayList<AccountBean>();
- list.add(bean);
- System.out.println("jsonGenerator");
- jsonGenerator.writeObject(list);
- System.out.println("\nObjectMapper");
- System.out.println("1###"+objectMapper.writeValueAsString(list));
- System.out.print("2###");
- objectMapper.writeValue(System.out,list);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- /**
- *@description:复杂的Java类型的JSON转换
- *输出:
- *{"user":{"name":"jackson","sex":true,"age":22},"infos":[22,"thisisarray"]}
- *{"user":{"id":1,"name":"haha","email":"email","address":"address","birthday":null},"infos":["a","b","c"]}
- *@returnvoid
- *@throws
- *@authorbeyond
- *@data:2016年2月24日下午1:08:06
- */
- @Test
- publicvoidwriteOthersJSON(){
- try{
- String[]arr={"a","b","c"};
- //Object
- jsonGenerator.writeStartObject();//{
- jsonGenerator.writeObjectFieldStart("user");//user:{
- jsonGenerator.writeStringField("name","jackson");//name:jackson
- jsonGenerator.writeBooleanField("sex",true);//sex:true
- jsonGenerator.writeNumberField("age",22);//age:22
- jsonGenerator.writeEndObject();//}
- jsonGenerator.writeArrayFieldStart("infos");//infos:[
- jsonGenerator.writeNumber(22);//22
- jsonGenerator.writeString("thisisarray");//thisisarray
- jsonGenerator.writeEndArray();//]
- jsonGenerator.writeEndObject();//}
- //********************************************
- AccountBeanbean=newAccountBean();
- bean.setAddress("address");
- bean.setEmail("email");
- bean.setId(1);
- bean.setName("haha");
- jsonGenerator.writeStartObject();//{
- jsonGenerator.writeObjectField("user",bean);//user:{bean}
- jsonGenerator.writeObjectField("infos",arr);//infos:[array]
- jsonGenerator.writeEndObject();//}
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
- /**
- *@description:将json字符串转换成JavaBean对象
- *用到了ObjectMapper这个对象的readValue这个方法,这个方法需要提供2个参数。
- *第一个参数就是解析的JSON字符串,
- *第二个参数是将这个JSON解析持什么Java对象,Java对象的类型。当然,还有其他相同签名方法。
- *
- *输出:
- *haha
- *haha#1#address#null#email
- *@returnvoid
- *@throws
- *@authorbeyond
- *@data:2016年2月24日下午3:37:50
- */
- @Test
- publicvoidreadJson2Entity(){
- Stringjson="{\"address\":\"address\",\"name\":\"haha\",\"id\":1,\"email\":\"email\"}";
- try{
- AccountBeanacc=objectMapper.readValue(json,AccountBean.class);
- System.out.println(acc.getName());
- System.out.println(acc);
- }catch(JsonParseExceptione){
- e.printStackTrace();
- }catch(JsonMappingExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- }
- &n
知识推荐
- CSS四种引入方式
- html常用标签
- 1、HTML+DIV+CSS零基础快速入门到制作企业站视频课程_15 css背景属性
- Extjs 基础篇—— Function 能在定义时就能执行的方法的写法 function(){...}()
- .Net中Remoting通信机制简单实例
- 查看php-fpm的进程和端口号
- 4 SDWebImageManager
- maven中json-lib库无法引入
- 【Linux网络编程】基于TCP流 I/O多路转接(poll) 的高性能http服务器
- Netty 高性能之道 - Recycler 对象池的复用
- Vue.js入门
- jQuery ajax方法success()中后台传来的四种数据类型
- d3js 画布 概念
- jQuery学习
- Triple transfer json
- 我的html自学之路--day1
- 0511JS基础练习
- fiddler抓取https的最终说明