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);
//反序列化時,遇到未知屬性會不會報(bào)錯
//true - 遇到?jīng)]有的屬性就報(bào)錯 false - 沒有的屬性不會管,不會報(bào)錯
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)文章
關(guān)于Spring自定義XML schema 擴(kuò)展的問題(Spring面試高頻題)
今天給大家分享一道spring高頻率面試題關(guān)于Spring自定義XML schema 擴(kuò)展的問題,今天以spring整合dubbo的實(shí)例給大家詳細(xì)講解下,感興趣的朋友跟隨小編一起看看吧2021-05-05
完美解決idea沒有tomcat server選項(xiàng)的問題
這篇文章主要介紹了完美解決idea沒有tomcat server選項(xiàng)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
IntelliJ IDEA中出現(xiàn)"PSI and index do not match"錯誤的解決辦法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中出現(xiàn)"PSI and index do not match"錯誤的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Spring Boot項(xiàng)目集成UidGenerato的方法步驟
這篇文章主要介紹了Spring Boot項(xiàng)目集成UidGenerato的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
使用spring+maven不同環(huán)境讀取配置方式
這篇文章主要介紹了使用spring+maven不同環(huán)境讀取配置方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08

