使用Jackson實(shí)現(xiàn)Map與Bean互轉(zhuǎn)方式
Jackson Map與Bean互轉(zhuǎn)
在使用 java 開(kāi)發(fā)中,通常需要把 Map 轉(zhuǎn)成 Bean,或把 Bean 轉(zhuǎn)成 Map,這就用的工具類,在此推薦使用import com.fasterxml.jackson.databind.ObjectMapper;包下的ObjectMapper類,比 JsonObject 效率高
下面就列舉了幾種使用方法
1、pom.xml
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency>
2、java 代碼
package com.jin.demo.jackson; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; /** * 使用 Jackson工具 * map to bean * bean to map * * @auther jinsx * @date 2019-03-26 16:37 */ public class JacksonTest { private static final ObjectMapper mapper = new ObjectMapper(); public static void main(String[] args) throws Exception { // 測(cè)試 將Map轉(zhuǎn)成指定的Bean Map<String, Object> map = new HashMap<>(); map.put("name", "張三"); Person o = (Person)JacksonTest.mapToBean(map, Person.class); System.out.println(o); // 測(cè)試 將Bean轉(zhuǎn)成Map Person person = new Person(); person.setAge(18); Map map1 = JacksonTest.beanToMap(person); System.out.println(map1); } // 將對(duì)象轉(zhuǎn)成字符串 public static String objectToString(Object obj) throws Exception { return mapper.writeValueAsString(obj); } // 將Map轉(zhuǎn)成指定的Bean public static Object mapToBean(Map map, Class clazz) throws Exception { return mapper.readValue(objectToString(map), clazz); } // 將Bean轉(zhuǎn)成Map public static Map beanToMap(Object obj) throws Exception { return mapper.readValue(objectToString(obj), Map.class); } }
3、Person 類
package com.jin.demo.jackson; import java.io.Serializable; import java.util.Date; /** * @auther jinsx * @date 2019-03-26 17:30 */ public class Person implements Serializable { private int age; private String name; private Date birthday; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "Person{" + "age=" + age + ", name='" + name + '\'' + ", birthday=" + birthday + '}'; } }
Jackson-ObjectMapper json字符串、bean、map、list互相轉(zhuǎn)換
1、對(duì)象轉(zhuǎn)json字符串
ObjectMapper mapper = new ObjectMapper(); User user = new User(); String userJson = mapper.writeValueAsString(user);
2、Map轉(zhuǎn)json字符串
ObjectMapper mapper = new ObjectMapper(); Map map = new HashMap(); String json = mapper.writeValueAsString(map);
3、數(shù)組list轉(zhuǎn)json字符串
ObjectMapper mapper = new ObjectMapper(); User[] uArr = {user1, user2}; String jsonfromArr = mapper.writeValueAsString(uArr);
4、json字符串轉(zhuǎn)對(duì)象
ObjectMapper mapper = new ObjectMapper(); String expected = "{\"name\":\"ZhangSan\"}"; User user = mapper.readValue(expected, User.class);
5、json字符串轉(zhuǎn)Map
ObjectMapper mapper = new ObjectMapper(); String expected = "{\"name\":\"ZhangSan\"}"; Map userMap = mapper.readValue(expected, Map.class);
6、json字符串轉(zhuǎn)對(duì)象數(shù)組List
ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"name\":\"Lisi\"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class); List<User> userList = mapper.readValue(expected, listType);
7、json字符串轉(zhuǎn)Map數(shù)組List<Map<String,Object>>
ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"nameEn\":\"Lisi\"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class); List<Map<String,Object>> userList = mapper.readValue(expected, listType);
8、jackson默認(rèn)將對(duì)象轉(zhuǎn)換為L(zhǎng)inkedHashMap
ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"nameEn\":\"Lisi\"},{\"name\":\"Test\"}]"; ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
9、json字符串轉(zhuǎn)對(duì)象設(shè)置格式
ObjectMapper mapper = new ObjectMapper(); //設(shè)置date轉(zhuǎn)換格式 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(fmt); String expected = "{\"name\":\"ZhangSan\",\"bir\":\"2021-04-15 14:00:00\"}"; User user = mapper.readValue(expected, User.class);
10、忽略未知字段
ObjectMapper mapper = new ObjectMapper(); //設(shè)置忽略未知字段 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); String expected = "{\"name\":\"ZhangSan\",\"job\":\"teacher\"}"; User user = mapper.readValue(expected, User.class);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java向文件中追加內(nèi)容與讀寫文件內(nèi)容源碼實(shí)例代碼
這篇文章主要介紹了java向文件中追加內(nèi)容與讀寫文件內(nèi)容源碼實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04java中的編碼轉(zhuǎn)換過(guò)程(以u(píng)tf8和gbk為例)
這篇文章主要介紹了java中的編碼轉(zhuǎn)換過(guò)程(以u(píng)tf8和gbk為例),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04spring cloud gateway 全局過(guò)濾器的實(shí)現(xiàn)
全局過(guò)濾器作用于所有的路由,不需要單獨(dú)配置,我們可以用它來(lái)實(shí)現(xiàn)很多統(tǒng)一化處理的業(yè)務(wù)需求,這篇文章主要介紹了spring cloud gateway 全局過(guò)濾器的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03SpringBoot各種參數(shù)校驗(yàn)的實(shí)例教程
經(jīng)常需要提供接口與用戶交互(獲取數(shù)據(jù)、上傳數(shù)據(jù)等),由于這個(gè)過(guò)程需要用戶進(jìn)行相關(guān)的操作,為了避免出現(xiàn)一些錯(cuò)誤的數(shù)據(jù)等,一般需要對(duì)數(shù)據(jù)進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于SpringBoot各種參數(shù)校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2022-03-03關(guān)于Java的ArrayList數(shù)組自動(dòng)擴(kuò)容機(jī)制
這篇文章主要介紹了關(guān)于Java的ArrayList數(shù)組自動(dòng)擴(kuò)容機(jī)制,ArrayList底層是基于數(shù)組實(shí)現(xiàn)的,是一個(gè)動(dòng)態(tài)數(shù)組,自動(dòng)擴(kuò)容,不是線程安全的,只能用在單線程環(huán)境下,需要的朋友可以參考下2023-05-05java設(shè)計(jì)模式學(xué)習(xí)之代理模式
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式學(xué)習(xí)之代理模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10MyBatis批量查詢、插入、更新、刪除的實(shí)現(xiàn)示例
由于需要處理短時(shí)間內(nèi)大量數(shù)據(jù)入庫(kù)的問(wèn)題,想到了Mybatis的批量操作,本文主要介紹了MyBatis批量查詢、插入、更新、刪除的實(shí)現(xiàn)示例,感興趣的可以了解一下2023-05-05