@JsonProperty的使用
生活随笔
收集整理的這篇文章主要介紹了
@JsonProperty的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
jackson的maven依賴
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.5.3</version> </dependency>@JsonProperty 此注解用于屬性上,作用是把該屬性的名稱序列化為另外一個名稱,如把trueName屬性序列化為name,@JsonProperty("name")。
?
import com.fasterxml.jackson.annotation.JsonProperty; public class Student { @JsonProperty("name") private String trueName; public String getTrueName() { return trueName; } public void setTrueName(String trueName) { this.trueName = trueName; } }
測試一下
?
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) throws JsonProcessingException { Student student = new Student(); student.setTrueName("張三"); System.out.println(new ObjectMapper().writeValueAsString(student)); } }?
得到結(jié)果
{"name":"張三"}
? ? 這里需要注意的是將對象轉(zhuǎn)換成json字符串使用的方法是fasterxml.jackson提供的!!
@JsonProperty不僅僅是在序列化的時候有用,反序列化的時候也有用,比如有些接口返回的是json字符串,命名又不是標(biāo)準(zhǔn)的駝峰形式,在映射成對象的時候,將類的屬性上加上@JsonProperty注解,里面寫上返回的json串對應(yīng)的名字
轉(zhuǎn)載于:https://www.cnblogs.com/toSeeMyDream/p/7885015.html
總結(jié)
以上是生活随笔為你收集整理的@JsonProperty的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年第10批 手机号正则表达式
- 下一篇: JavaScript--jquery.m