引用jar:jackson-core,jackson-databind,jackson-annotationsjava
http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.9.9/jackson-core-2.9.9.jarjson
一、jackson基本使用maven
1.一、建立Person對象this
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.二、Main方法調用spa
備註:對象轉json須要屬性擁有get方法(註解方法除外)code
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)); } }
輸出結果爲:orm
{"name":"jackson"}
二、註解使用xml
2.一、@JsonProperty註解使用
@JsonProperty註解相似於sql裏字段的別名,用於序列化,使用註解字段屬性,替代原字段屬性
@JsonProperty("userName") private String name;
序列化結果爲:在序列化的json串中,userName替代了name
{"userName":"jackson"}
2.二、@JsonIgnore註解使用
@JsonIgnore註解是在序列化時忽略該字段
@JsonIgnore @JsonProperty("userName") private String name; @JsonProperty("userAge") private Integer age;
序列化結果爲:{"userAge":20}
{"userAge":20}
2.三、@JsonIgnoreProperties註解使用
2.3.一、序列化@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.二、@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.四、@JsonTypeName @JsonTypeInfo
@JsonTypeName(value = "user")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
在序列化是增長一層
序列化結果爲:{"user":{"height":175}}
{"user":{"height":175}}
2.五、@JsonRootName註解
2.4組合在序列化上等於類上註解@JsonRootName("user") 和 mapper.enable(SerializationFeature.WRAP_ROOT_VALUE),反序列化無用;
2.六、@JsonFormat註解格式化日期格式
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss:SSS",timezone="GMT+8")
private Date date;
2.七、@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.八、@JsonAnySetter註解
@JsonAnySetter註解主要做用於反序列化上:
1.用在非靜態方法上,註解的方法必須有兩個參數,第一個是json字段中的key,第二個是value,方法名隨意
2.反序列化的時候將對應不上的字段所有放到Map裏面
2.九、JavaType
list反序列化爲LinkedHashMap若是要轉換爲原來對象類型
若是爲Map類型 mapper.getTypeFactory().constructParametricType(Map.class,String.class,Student.class);
若是爲List類型 personList = mapper.readValue(mapper.writeValueAsString(personList),mapper.getTypeFactory().constructParametricType(List.class,Person.class));// 第二個參數是Map的key,第三個參數是Map的value
2.十、TypeReference
TypeReference比javaType模式更加方便,代碼也更加簡潔
mapper.readValue(json, new TypeReference<List<Person>>(){});
三、序列化(SerializationFeature)與反序列化(DeserializationFeature)自定義規則