Jackson使用手冊


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

http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.9.9/jackson-annotations-2.9.9.jar

http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.9.9/jackson-core-2.9.9.jar

http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.9.9/jackson-databind-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)自定義規則


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM