java jackson 手册_Jackson使用手册

引用jar:jackson-core,jackson-databind,jackson-annotations

1、jackson基本使用

1.1、创建Person对象

public classPerson {privateString name;privateInteger age;publicPerson(String name, Integer age) {this.name =name;this.age =age;

}publicString getName() {returnname;

}

}

1.2、Main方法调用

备注:对象转json需要属性拥有get方法(注解方法除外)

importcom.fasterxml.jackson.databind.ObjectMapper;public classMain {public static void main(String[] arges) throwsException {

ObjectMapper mapper= newObjectMapper();

Person person= new Person("jackson",20);

System.out.println(mapper.writeValueAsString(person));

}

}

输出结果为:

{"name":"jackson"}

2、注解使用

2.1、@JsonProperty注解使用

@JsonProperty注解类似于sql里字段的别名,用于序列化,使用注解字段属性,替代原字段属性

@JsonProperty("userName")private String name;

序列化结果为:在序列化的json串中,userName替代了name

{"userName":"jackson"}

2.2、@JsonIgnore注解使用

@JsonIgnore注解是在序列化时忽略该字段

@JsonIgnore

@JsonProperty("userName")privateString name;

@JsonProperty("userAge")private Integer age;

序列化结果为:{"userAge":20}

{"userAge":20}

2.3、@JsonIgnoreProperties注解使用

2.3.1、序列化@JsonIgnoreProperties与@JsonIgnore类似,用于类上,注解使用的是字段别名

importcom.fasterxml.jackson.annotation.JsonIgnore;importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;importcom.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(value={"name","userAge"})public classPerson {

@JsonIgnore

@JsonProperty("userName")privateString name;

@JsonProperty("userAge")privateInteger age;

@JsonProperty("userHeight")privateInteger height;publicPerson(String name, Integer age, Integer height) {this.name =name;this.age =age;this.height =height;

}

}

ObjectMapper mapper = newObjectMapper();

Person person= new Person("jackson",20,175);

System.out.println(mapper.writeValueAsString(person));

运行结果为:{"userHeight":175}

{"userHeight":175}

2.3.2、@JsonIgnoreProperties(ignoreUnknown = true)用于忽略字段不匹配情况,相当于mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

备注:反序列化需要有无参构造器

ObjectMapper mapper = newObjectMapper();

Person person= new Person("jackson",20,175);

System.out.println(mapper.writeValueAsString(person));//mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

System.out.println(mapper.readValue("{\"sheight\":172}", Person.class).getHeight());

2.4、@JsonTypeName @JsonTypeInfo

@JsonTypeName(value = "user")

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)

在序列化是增加一层

序列化结果为:{"user":{"height":175}}

{"user":{"height":175}}

2.5、@JsonRootName注解

2.4组合在序列化上等于类上注解@JsonRootName("user") 和 mapper.enable(SerializationFeature.WRAP_ROOT_VALUE),反序列化无用;

2.6、@JsonFormat注解格式化日期格式

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss:SSS",timezone="GMT+8")

private Date date;

2.7、@JsonAnyGetter注解

@JsonAnyGetter注解主要用在序列化:

1.方法是非静态,没有参数的,方法名随意

2.方法返回值必须是Map类型

3.在一个实体类中仅仅用在一个方法上

4.序列化的时候json字段的key就是返回Map的key,value就是Map的value

//@JsonAnyGetter

public MapgetMap(){returnmap;

}

序列化结果为:{"height":175,"map":{"1":"1","key":"value"},"date":"2019-05-29 10:37:55:759"}

取消注释后序列化结果为:{"height":175,"date":"2019-05-29 10:39:14:685","1":"1","key":"value"}

2.8、@JsonAnySetter注解

@JsonAnySetter注解主要作用于反序列化上:

1.用在非静态方法上,注解的方法必须有两个参数,第一个是json字段中的key,第二个是value,方法名随意

2.反序列化的时候将对应不上的字段全部放到Map里面

2.9、JavaType

list反序列化为LinkedHashMap如果要转换为原来对象类型

如果为Map类型 mapper.getTypeFactory().constructParametricType(Map.class,String.class,Student.class);// 第二个参数是Map的key,第三个参数是Map的value

如果为List类型 personList= mapper.readValue(mapper.writeValueAsString(personList),mapper.getTypeFactory().constructParametricType(List.class,Person.class));

2.10、TypeReference

TypeReference比javaType模式更加方便,代码也更加简洁

mapper.readValue(json, new TypeReference>(){});

3、序列化(SerializationFeature)与反序列化(DeserializationFeature)自定义规则

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值