Java ObjectMapper使用詳解
簡介
ObjectMapper類(com.fasterxml.jackson.databind.ObjectMapper)是Jackson的主要類,它可以幫助我們快速的進(jìn)行各個類型和Json類型的相互轉(zhuǎn)換。
使用
1、引入Jackson的依賴
<!-- 根據(jù)自己需要引入相關(guān)版本依賴。 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.10</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.10</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.10</version> </dependency>
2、ObjectMapper的常用配置
private static final ObjectMapper mapper;
public static ObjectMapper getObjectMapper(){
return this.mapper;
}
static{
//創(chuàng)建ObjectMapper對象
mapper = new ObjectMapper()
//configure方法 配置一些需要的參數(shù)
// 轉(zhuǎn)換為格式化的json 顯示出來的格式美化
mapper.enable(SerializationFeature.INDENT_OUTPUT);
//序列化的時候序列對象的那些屬性
//JsonInclude.Include.NON_DEFAULT 屬性為默認(rèn)值不序列化
//JsonInclude.Include.ALWAYS 所有屬性
//JsonInclude.Include.NON_EMPTY 屬性為 空(“”) 或者為 NULL 都不序列化
//JsonInclude.Include.NON_NULL 屬性為NULL 不序列化
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
//反序列化時,遇到未知屬性會不會報錯
//true - 遇到?jīng)]有的屬性就報錯 false - 沒有的屬性不會管,不會報錯
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//如果是空對象的時候,不拋異常
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// 忽略 transient 修飾的屬性
mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
//修改序列化后日期格式
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//處理不同的時區(qū)偏移格式
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JavaTimeModule());
}3、ObjectMapper的常用方法
a.json字符串轉(zhuǎn)對象
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"Hyl\", \"age\":20}";
//將字符串轉(zhuǎn)換為對象
Student student = mapper.readValue(jsonString, Student.class);
System.out.println(student);
//將對象轉(zhuǎn)換為json字符串
jsonString = mapper.writeValueAsString(student);
System.out.println(jsonString);
結(jié)果:
Student [ name: Hyl, age: 20 ]
{
"name" : "Hyl",
"age" : 20
}b. 數(shù)組和對象之間轉(zhuǎn)換
//對象轉(zhuǎn)為byte數(shù)組 byte[] byteArr = mapper.writeValueAsBytes(student); System.out.println(byteArr); //byte數(shù)組轉(zhuǎn)為對象 Student student= mapper.readValue(byteArr, Student.class); System.out.println(student); 結(jié)果: [B@3327bd23 Student [ name: Hyl, age: 20 ]
c. 集合和json字符串之間轉(zhuǎn)換
List<Student> studentList= new ArrayList<>();
studentList.add(new Student("hyl1" ,20 , new Date()));
studentList.add(new Student("hyl2" ,21 , new Date()));
studentList.add(new Student("hyl3" ,22 , new Date()));
studentList.add(new Student("hyl4" ,23 , new Date()));
String jsonStr = mapper.writeValueAsString(studentList);
System.out.println(jsonStr);
List<Student> studentList2 = mapper.readValue(jsonStr, List.class);
System.out.println("字符串轉(zhuǎn)集合:" + studentList2 );
結(jié)果:
[ {
"name" : "hyl1",
"age" : 20,
"sendTime" : 1525164212803
}, {
"name" : "hyl2",
"age" : 21,
"sendTime" : 1525164212803
}, {
"name" : "hyl3",
"age" : 22,
"sendTime" : 1525164212803
}, {
"name" : "hyl4",
"age" : 23,
"sendTime" : 1525164212803
} ]
[{name=hyl1, age=20, sendTime=1525164212803}, {name=hyl2, age=21, sendTime=1525164212803}, {name=hyl3, age=22, sendTime=1525164212803}, {name=hyl4, age=23, sendTime=1525164212803}]d. map和json字符串之間轉(zhuǎn)換
Map<String, Object> testMap = new HashMap<>();
testMap.put("name", "22");
testMap.put("age", 20);
testMap.put("date", new Date());
testMap.put("student", new Student("hyl", 20, new Date()));
String jsonStr = mapper.writeValueAsString(testMap);
System.out.println(jsonStr);
Map<String, Object> testMapDes = mapper.readValue(jsonStr, Map.class);
System.out.println(testMapDes);
結(jié)果:
{
"date" : 1525164212803,
"name" : "22",
"student" : {
"name" : "hyl",
"age" : 20,
"sendTime" : 1525164212803,
"intList" : null
},
"age" : 20
}
{date=1525164212803, name=22, student={name=hyl, age=20, sendTime=1525164212803, intList=null}, age=20}e. 日期轉(zhuǎn)json字符串
// 修改時間格式
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
Student student = new Student ("hyl",21, new Date());
student.setIntList(Arrays.asList(1, 2, 3));
String jsonStr = mapper.writeValueAsString(student);
System.out.println(jsonStr);
結(jié)果:
{
"name" : "hyl",
"age" : 21,
"sendTime" : "2020-07-23 13:14:36",
"intList" : [ 1, 2, 3 ]
}js中將字符串轉(zhuǎn)換為json對象
var data = "{\"name\":\"Hyl\", \"age\":20}";
var student = eval(data);
console.info(student.name);
console.info(student.age);
結(jié)果:
Hyl
20到此這篇關(guān)于Java ObjectMapper詳解的文章就介紹到這了,更多相關(guān)Java ObjectMapper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談spring使用策略模式實現(xiàn)多種場景登錄方式
本文主要介紹了spring使用策略模式實現(xiàn)多種場景登錄方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Java GUI進(jìn)階之流式布局管理器FlowLayout專項精講
FlowLayout-流式布局管理器,按水平方向依次排列放置組件,排滿一行,換下一行繼續(xù)排列。排列方向(左到右 或 右到左)取決于容器的componentOrientation屬性2022-04-04
Java數(shù)據(jù)結(jié)構(gòu)之隊列示例詳解
隊列是一種線性數(shù)據(jù)結(jié)構(gòu),它遵循先進(jìn)先出或后近后出的原則,隊列允許在一端插入元素,另一端刪除元素,這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之隊列的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-12-12

