jackson 快速开始
发布日期:2021-06-29 22:24:15 浏览次数:2 分类:技术文章

本文共 4598 字,大约阅读时间需要 15 分钟。

文章目录

jackson 介绍

官方地址: https://github.com/FasterXML/jackson-core

常用使用方法

maven引入

2.9.9
...
com.fasterxml.jackson.core
jackson-core
${jackson-version}
com.fasterxml.jackson.core
jackson-databind
${jackson-version}
com.fasterxml.jackson.core
jackson-annotations
${jackson-version}

JAVA对象转JSON[JSON序列化]

ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。

ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。

  1. writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
  2. writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
  3. writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
  4. writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。

JSON转Java类[JSON反序列化]

JSON转对象

objectMapping.readValue(str, ojb.class)
  • 如果json字符串中的属性个数小于java对象中的属性个数,可以顺利转换,java中多的那个属性为null

  • 如果json字符串中出现java对象中没有的属性,则在将json转换为java对象时会报错:Unrecognized field, not marked as ignorable

demo

public class JacksonTest {
public static void main(String args[]) {
Student student = new Student(); student.setAge(20); student.setName("she"); ObjectMapper mapper = new ObjectMapper(); try {
//Java类转JSON String json = mapper.writeValueAsString(student); System.out.println(json); } catch (JsonProcessingException e) {
e.printStackTrace(); } //Java集合转JSON List
studentList = new ArrayList
(); studentList.add(student); String jsonlist = null; try {
jsonlist = mapper.writeValueAsString(studentList); } catch (JsonProcessingException e) {
e.printStackTrace(); } System.out.println(jsonlist); String json = "{\"name\":\"she\",\"age\":20}"; /** * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。 */ ObjectMapper mapper1 = new ObjectMapper(); Student student1 = null; try {
student1 = mapper1.readValue(json, Student.class); } catch (IOException e) {
e.printStackTrace(); } System.out.println(student1); } static class Student {
private String name; private int age; public Student() {
} public String getName() {
return name; } public void setName(String name) {
this.name = name; } public int getAge() {
return age; } public void setAge(int age) {
this.age = age; } }}

SpringBoot Jackson

SpringBoot JSON工具包默认是Jackson,只需要引入spring-boot-starter-web依赖包,自动引如下相应依赖包:

com.fasterxml.jackson.core
jackson-databind
-->数据绑定依赖于下面两个包
2.8.7
com.fasterxml.jackson.core
jackson-annotations
-->注解包
2.8.0
com.fasterxml.jackson.core
jackson-core
-->核心包
2.8.7

Jackson两种配置方式

  1. application.properties文件
# 日期格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss# 日期时区spring.jackson.time-zone=GMT+8# 返回值null不显示spring.jackson.default-property-inclusion=non_null
  1. bean配置
import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;@Configurationpublic class JacksonConfig {
@Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 返回值过滤null或""值 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY).setSerializationInclusion(JsonInclude.Include.NON_NULL); return objectMapper; }}

转载地址:https://docker.blog.csdn.net/article/details/105747959 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:OKHttp3 快速开始
下一篇:java socket-keepalive理解

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月21日 12时30分23秒