本文将介绍几种不同的方法来实现Java
对象和Map
之间的相互转换,选择哪种方法取决于项目的具体需求和个人偏好。
方法一:使用Spring Framework的ReflectionUtils
Bean转为Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公众号");
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
public static Map bean2Map(Object object) {
Map map = new HashMap<>();
ReflectionUtils.doWithFields(object.getClass(), field -> {
field.setAccessible(true);
Object value = ReflectionUtils.getField(field, object);
if (value != null) {
map.put(field.getName(), value);
}
});
return map;
}
public static Map bean2Map2(Object object) {
Map map = new HashMap<>();
Class> clazz = object.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object value = field.get(object);
if (value != null) {
map.put(field.getName(), value);
}
} catch (IllegalAccessException e) {
throw new RuntimeException("Error accessing field: " + field.getName(), e);
}
}
return map;
}
Map转为Bean
Map map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公众号");
System.out.println(map2Bean(map, Person.class));
System.out.println(map2Bean2(map, Person.class));
public static T map2Bean(Map map, Class clazz) throws IllegalAccessException, InstantiationException {
T instance = clazz.newInstance();
ReflectionUtils.doWithFields(clazz, field -> {
field.setAccessible(true);
if (map.containsKey(field.getName())) {
ReflectionUtils.setField(field, instance, map.get(field.getName()));
}
});
return instance;
}
public static T map2Bean2(Map map, Class clazz) throws IllegalAccessException, InstantiationException {
T instance = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (map.containsKey(field.getName())) {
field.set(instance, map.get(field.getName()));
}
}
return instance;
}
方法二:使用Hutool工具
cn.hutool
hutool-all
5.8.5
Bean转为Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公众号");
Map map = BeanUtil.beanToMap(person);
Map转为Bean
Map map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公众号");
Person person = BeanUtil.toBean(map, Person.class);
方法三:使用Jackson工具
Bean转为Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公众号");
System.out.println(bean2Map(person));
public static Map bean2Map(Object object) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(object, new TypeReference
Map转为Bean
Map map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公众号");
System.out.println(map2Bean(map, Person.class));
public static T map2Bean(Map map, Class clazz){
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.convertValue(map, clazz);
}
方法四:使用Apache Commons Lang的BeanUtils
org.apache.commons
commons-lang3
3.12.0
Bean转为Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公众号");
System.out.println(bean2Map(person));
public static Map bean2Map(Object object) {
try {
return BeanUtils.describe(object);
} catch (Exception e) {
throw new RuntimeException("Error converting object to map: " + e.getMessage(), e);
}
}
Map转为Bean
Map map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公众号");
System.out.println(map2Bean(map, Person.class));
public static T map2Bean(Map map, Class clazz) {
try {
T instance = clazz.newInstance();
BeanUtils.populate(instance, map);
return instance;
} catch (Exception e) {
throw new RuntimeException("Error converting map to object: " + e.getMessage(), e);
}
}
方法五:使用fastjson工具
com.alibaba
fastjson
1.2.83
Bean转为Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公众号");
System.out.println(JSONObject.parseObject(JSONObject.toJSONString(person)));
Map转为Bean
Map map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公众号");
System.out.println(JSONObject.parseObject(JSONObject.toJSONString(map), Person.class));