Jackson使用手册
引用jar:jackson-core,jackson-databind,jackson-annotations
http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.9.9/jackson-core-2.9.9.jar
1、jackson基本使用
1.1、创建Person对象
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
}
1.2、Main方法调用
备注:对象转json需要属性拥有get方法(注解方法除外)
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] arges) throws Exception {
ObjectMapper mapper = new ObjectMapper();
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")
private String name;
@JsonProperty("userAge")
private Integer age;
序列化结果为:{"userAge":20}
{"userAge":20}
2.3、@JsonIgnoreProperties注解使用
2.3.1、序列化@JsonIgnoreProperties与@JsonIgnore类似,用于类上,注解使用的是字段别名
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(value={"name","userAge"})
public class Person {
@JsonIgnore
@JsonProperty("userName")
private String name;
@JsonProperty("userAge")
private Integer age;
@JsonProperty("userHeight")
private Integer height; public Person(String name, Integer age, Integer height) {
this.name = name;
this.age = age;
this.height = height;
}
}
ObjectMapper mapper = new ObjectMapper();
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 = new ObjectMapper();
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 Map<String,Object> getMap(){
return map;
}
序列化结果为:{"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<List<Person>>(){});
3、序列化(SerializationFeature)与反序列化(DeserializationFeature)自定义规则
Jackson使用手册的更多相关文章
- Storm官方帮助手册翻译(上)
Storm作为当前最流行的实时计算框架,自Twitter将其开源后就一直备受关注.由于其具有先天的稳定性以及便捷性,目前被许多大公司所采用,国外像雅虎.雅虎日本.Twitter.OOYALA.Spot ...
- Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】
源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...
- Vert.x Core 文档手册
Vert.x Core 文档手册 中英对照表 Client:客户端 Server:服务器 Primitive:基本(描述类型) Writing:编写(有些地方译为开发) Fluent:流式的 Reac ...
- 阿里巴巴Java开发手册正确学习姿势是怎样的?刷新代码规范认知
很多人都知道,阿里巴巴在2017发布了<阿里巴巴Java开发手册>,前后推出了很多个版本,并在后续推出了与之配套的IDEA插件和书籍. 相信很多Java开发都或多或少看过这份手册,这份手册 ...
- FREERTOS 手册阅读笔记
郑重声明,版权所有! 转载需说明. FREERTOS堆栈大小的单位是word,不是byte. 根据处理器架构优化系统的任务优先级不能超过32,If the architecture optimized ...
- JS魔法堂:不完全国际化&本地化手册 之 理論篇
前言 最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...
- 转职成为TypeScript程序员的参考手册
写在前面 作者并没有任何可以作为背书的履历来证明自己写作这份手册的分量. 其内容大都来自于TypeScript官方资料或者搜索引擎获得,期间掺杂少量作者的私见,并会标明. 大部分内容来自于http:/ ...
- Redis学习手册(目录)
为什么自己当初要选择Redis作为数据存储解决方案中的一员呢?现在能想到的原因主要有三.其一,Redis不仅性能高效,而且完全免费.其二,是基于C/C++开发的服务器,这里应该有一定的感情因素吧.最后 ...
- jackson简单使用,对象转json,json转对象,json转list
添加jackson依赖: // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core compile g ...
随机推荐
- Flask16 项目结构、flask_script插件
1 项目结构 需求:易维护.可扩展 1.1 views 处理逻辑和路由映射 C 1.2 models 模型类 M 1.3 templates 模板文件 V 1.4 static 今天文件,如:js.c ...
- R语言中的字符处理
R语言中的字符处理 (2011-07-10 22:29:48) 转载▼ 标签: r语言 字符处理 字符串 连接 分割 分类: R R的字符串处理能力还是很强大的,具体有base包的几个函数和strin ...
- hdu1083
#include"stdio.h" #include"string.h" #define N 305 int mark[N],link[N],map[N][N] ...
- EasyUI知识点汇总
combobox联动设置 $(document).ready(function() { $('#gdfsdl1').combobox({ onSelect: function(record){ $(' ...
- vue入门(一)----组件
由于工作需要,最近在写一些前端的东西.经过向开发经验丰富的前端工程师的请教之后,得出一个结论----2016年前端被玩坏了,其实对于我来说我并不是太了解这句话的深刻含义,只是觉得是不是因为前端技术突飞 ...
- JS中的for....in循环 和 for ...of循环以及iterable遍历Map和Set
for循环的一个变体是for ... in循环,它可以把一个对象的所有属性依次循环出来: var o = { name: 'Jack', age: 20, city: 'Beijing' }; for ...
- Codeforces Round #522 Div2C(思维)
#include<bits/stdc++.h>using namespace std;int a[200007];int b[200007][7];int ans[200007];int ...
- 最小生成树(prim和kruskal)
最小生成树(prim和kruskal) 最小生成树的最优子结构性质 设一个最小生成树是T.如果选出一个T中的一条边,分裂成的两个树T1,T2依然是它们的点集组成的最小生成树.这可以用反证法来证.反着来 ...
- Mol Cell Proteomics. |马臻| psims-一个用于编写HUPO-PSI标准下的mzML和mzIdentML的python库
大家好,本周分享的是发表在MCP(MOLECULAR&CRLLULAR PROTEOMICS)上的一篇关于质谱数据处理和识别的文章,题目是psims - A Declarative Write ...
- giihub上的关于js的43道题目
参考 https://github.com/lydiahallie/javascript-questions