
Jackson是Spring Boot默認的JSON資料處理框架,但是其并不依賴于任何的Spring 庫,有的小伙伴以為Jackson只能在Spring框架內使用,其實不是的,沒有這種限制,它提供了很多的JSON資料處理方法、注解,也包括流式API、樹模型、資料系結,以及復雜資料型別轉換等功能,它雖然簡單易用,但絕對不是小玩具,本節為大家介紹Jackson的基礎核心用法,更多的內容我會寫成一個系列,5-10篇文章,請您繼續關注我,
一、基礎準備
在任意專案中引入下面的jar就可以使用jackson進行JSON的資料序列化與反序列化的功能,
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
寫一個PlayerStar的物體類,物體類主要體現籃球明星的名字、年齡、業余愛好、朋友、年收入等資訊,為了盡可能地演示Jackson的序列化與反序列化功能,將陣列、List、Map都融合到這個類里面,并通過getInstance初始化籃球明星Jordan這個物件,
@Data
public class PlayerStar {
private String name;
private Integer age;
private String[] hobbies; //業余愛好,陣列
private List<String> friends; // 朋友
private Map<String, BigDecimal> salary; //年收入 Map
//初始化一個物件用于測驗
public static PlayerStar getInstance(){
PlayerStar playerStar = new PlayerStar();
playerStar.setName("喬丹");
playerStar.setAge(45);
playerStar.setHobbies(new String[]{"高爾夫球", "棒球"});
Map<String, BigDecimal> salary = new HashMap<String, BigDecimal>() {{
put("2000", new BigDecimal(10000000));
put("2010", new BigDecimal(62000000));
put("2020", new BigDecimal(112400000));
}};
playerStar.setSalary(salary);
playerStar.setFriends(Arrays.asList("kobe", "curry", "james"));
return playerStar;
}
}
二、序列化方法
下面代碼演示了如何將PlayerStar物件序列化為JSON字串,
- writeValue可以接收File作為引數,將JSON序列化結果保存到檔案中
- writeValueAsString將JSON序列化結果以String形式回傳
- writerWithDefaultPrettyPrinter方法可以將JSON序列化結果進行格式化,更好的顯示結構,易于查看
@Test
void testObject2JSON() throws IOException {
//獲取物件實體
PlayerStar player = PlayerStar.getInstance();
//ObjectMapper作為Jackson的API工具類存在
ObjectMapper mapper = new ObjectMapper();
//將player物件以JSON格式進行序列化,并將序列化結果寫入檔案
mapper.writeValue(new File("d:\\data\\jackson\\player.json"), player);
//將player物件以JSON格式進行序列化為String物件
String jsonString = mapper.writeValueAsString(player);
System.out.println(jsonString);
//將player物件以JSON格式進行序列化為String物件(格式美化)
String jsonInString2 = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(player);
System.out.println(jsonInString2);
}
jsonString的控制臺列印輸出結果,也是d:\data\jackson\player.json檔案的內容
{"name":"喬丹","age":45,"hobbies":["高爾夫球","棒球"],"friends":["kobe","curry","james"],"salary":{"2000":10000000,"2010":62000000,"2020":112400000}}
jsonString2的控制臺列印輸出,格式進行了美化,因為使用了writerWithDefaultPrettyPrinter()方法
{
"name" : "喬丹",
"age" : 45,
"hobbies" : [ "高爾夫球", "棒球" ],
"friends" : [ "kobe", "curry", "james" ],
"salary" : {
"2000" : 10000000,
"2010" : 62000000,
"2020" : 112400000
}
}
三、反序列化方法
下面代碼演示了如何將JSON字串反序列化為Java物件
@Test
void testJSON2Object() throws IOException {
ObjectMapper mapper = new ObjectMapper();
//從檔案中讀取JSON字串,反序列化為java物件
PlayerStar player = mapper.readValue(new File("d:\\data\\jackson\\player.json"), PlayerStar.class);
System.out.println(player);
//將JSON字串反序列化為java物件
String jsonInString = "{\"name\":\"喬丹\",\"age\":45,\"hobbies\":[\"高爾夫球\",\"棒球\"]}";
PlayerStar jordan = mapper.readValue(jsonInString, PlayerStar.class);
System.out.println(jordan);
}
PlayerStar物件控制臺輸出結果如下(注意這里的輸出不是JSON格式,而是java物件的toString()方法值):
PlayerStar(name=喬丹, age=45, hobbies=[高爾夫球, 棒球], friends=[kobe, curry, james], salary={2000=10000000, 2010=62000000, 2020=112400000})
PlayerStar(name=喬丹, age=45, hobbies=[高爾夫球, 棒球], friends=null, salary=null)
四、欄位重命名 @JsonProperty
可以使用 @JsonProperty來影響序列化和反序列化物件屬性的重命名,
@Data
public class PlayerStar {
@JsonProperty("playerName")
private String name; //將屬性name序列化為playerName,同時影響反序列化
使用上面代碼的注解之后,JSON序列化的結果name屬性變成playerName屬性
{"playerName":"喬丹" ……
同時影響反序列化,下面的反序列化代碼會報錯,因為使用了name屬性,應該使用playerName才可以,
String jsonInString = "{\"name\":\"喬丹\",\"age\":45,\"hobbies\":[\"高爾夫球\",\"棒球\"]}";
PlayerStar jordan = mapper.readValue(jsonInString, PlayerStar.class);
五、忽略null欄位的序列化@JsonInclude
當我們不為物件的成員變數賦值的時候,默認情況下,Jackson的序列化結果是下面的這樣的,
{
"age" : 45,
"hobbies" : null,
"friends" : null,
"salary" : null,
"playerName" : "喬丹"
}
如果我們不希望將null值,體現在JSON序列化結果中,我們可以使用下面的方法,如果希望在某次序列化的全域范圍內,忽略null成員變數,可以使用下面的API
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
或者是在類名的上面加上如下注解,該注解將針對類里面的所有成員變數生效,只要成員變數為null,將不會被包含在序列化結果中,
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PlayerStar {
......
}
如果我們想針對PlayerStar類里面某些成員變數單獨忽略null,可以在成員變數上面加注解,
@JsonInclude(JsonInclude.Include.NON_NULL)
private String[] hobbies; //業余愛好,陣列
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<String> friends; // 朋友
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, BigDecimal> salary; //年收入 Map
忽略為null的成員變數后,JSON序列化結果是下面這樣的
{
"age" : 45,
"playerName" : "喬丹"
}
六、忽略指定的欄位
默認情況下,jackson不會將static和transient的成員變數進行序列化與反序列化操作,我們還可以通過
@JsonIgnore加在類成員變數上面,該成員變數將被排除在序列化和反序列化的程序之外@JsonIgnoreProperties加在類宣告上面,指定該類里面哪些欄位被排除在序列化和反序列化的程序之外
上面的兩種注解選其一即可,下面的代碼兩種注解我都用了,功能是重復的
@Data
@JsonIgnoreProperties({"hobbies", "friends","salary"})
public class PlayerStar {
@JsonProperty("playerName")
private String name;
private Integer age;
@JsonIgnore
private String[] hobbies; //業余愛好,陣列
@JsonIgnore
private List<String> friends; // 朋友
@JsonIgnore
private Map<String, BigDecimal> salary; //年收入 Map
......
在類或成員變數上面加上注解之后,序列化結果如下,指定欄位被忽略,
{
"age" : 45,
"playerName" : "喬丹"
}
需要注意的是這兩個注解不只是影響序列化為JSON字串的程序,也影響JSON字串反序列化為java物件的程序,舉例:如果JSON字串包含了類中被JsonIgnore的屬性值hobbies,不會被反序列化賦值給java物件的成員變數hobbies,
歡迎關注我的博客,里面有很多精品合集
- 本文轉載注明出處(必須帶連接,不能只轉文字):字母哥博客,
覺得對您有幫助的話,幫我點贊、分享!您的支持是我不竭的創作動力! ,另外,筆者最近一段時間輸出了如下的精品內容,期待您的關注,
- 《手摸手教你學Spring Boot2.0》
- 《Spring Security-JWT-OAuth2一本通》
- 《實戰前后端分離RBAC權限管理系統》
- 《實戰SpringCloud微服務從青銅到王者》
- 《VUE深入淺出系列》
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/64336.html
標籤:Java
上一篇:全域監聽路由堆疊變化
