您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用Jackson實現(xiàn)Map與Bean互轉(zhuǎn)方式”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
在使用 java 開發(fā)中,通常需要把 Map 轉(zhuǎn)成 Bean,或把 Bean 轉(zhuǎn)成 Map,這就用的工具類,在此推薦使用import com.fasterxml.jackson.databind.ObjectMapper;包下的ObjectMapper類,比 JsonObject 效率高
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency>
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 { // 測試 將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); // 測試 將Bean轉(zhuǎn)成Map Person person = new Person(); person.setAge(18); Map map1 = JacksonTest.beanToMap(person); System.out.println(map1); } // 將對象轉(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); } }
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 + '}'; } }
ObjectMapper mapper = new ObjectMapper(); User user = new User(); String userJson = mapper.writeValueAsString(user);
ObjectMapper mapper = new ObjectMapper(); Map map = new HashMap(); String json = mapper.writeValueAsString(map);
ObjectMapper mapper = new ObjectMapper(); User[] uArr = {user1, user2}; String jsonfromArr = mapper.writeValueAsString(uArr);
ObjectMapper mapper = new ObjectMapper(); String expected = "{\"name\":\"ZhangSan\"}"; User user = mapper.readValue(expected, User.class);
ObjectMapper mapper = new ObjectMapper(); String expected = "{\"name\":\"ZhangSan\"}"; Map userMap = mapper.readValue(expected, Map.class);
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);
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);
ObjectMapper mapper = new ObjectMapper(); String expected = "[{\"name\":\"Zhangsan\"},{\"nameEn\":\"Lisi\"},{\"name\":\"Test\"}]"; ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
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);
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);
“怎么用Jackson實現(xiàn)Map與Bean互轉(zhuǎn)方式”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。