使用Jackson實現(xiàn)Map與Bean互轉(zhuǎn)方式
Jackson Map與Bean互轉(zhuǎn)
在使用 java 開發(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 {
// 測試 將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);
}
}
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、對象轉(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)對象
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)對象數(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)將對象轉(zhuǎn)換為LinkedHashMap
ObjectMapper mapper = new ObjectMapper();
String expected = "[{\"name\":\"Zhangsan\"},{\"nameEn\":\"Lisi\"},{\"name\":\"Test\"}]";
ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
9、json字符串轉(zhuǎn)對象設(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);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot關(guān)于自定義stater的yml無法提示問題解決方案
這篇文章主要介紹了Springboot關(guān)于自定義stater的yml無法提示問題及解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
如何用Java來進行文件切割和簡單的內(nèi)容過濾的實現(xiàn)
這篇文章主要介紹了如何用Java來進行文件切割和簡單的內(nèi)容過濾的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(三)之MyBatis全局配置文件解析
這篇文章主要介紹了Spring+SpringMVC+MyBatis深入學(xué)習(xí)及搭建(三)之MyBatis全局配置文件解析,需要的朋友可以參考下2017-05-05
SpringBoot的WebSocket實現(xiàn)單聊群聊
這篇文章主要為大家詳細(xì)介紹了SpringBoot的WebSocket實現(xiàn)單聊群聊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02
SSM+微信小程序?qū)崿F(xiàn)物業(yè)管理系統(tǒng)及實例代碼
這篇文章主要介紹了SSM+微信小程序?qū)崿F(xiàn)物業(yè)管理系統(tǒng),ssm微信小程序物業(yè)管理系統(tǒng),有網(wǎng)站后臺管理系統(tǒng),本文通過實例代碼給大家展示系統(tǒng)的功能,需要的朋友可以參考下2022-02-02

