如何在Java中实现对象和Map之间的转换

如何在Java中实现对象和Map之间的转换原创一安 2024 年 09 月 03 日 07 01 北京大家好 我是一安 在 Java 开发中 经常需要将 Java 对象转换成 Map 或者反过来将 Map 转换成 Java 对象

大家好,欢迎来到IT知识分享网。

原创 一安 一安未来 2024年09月03日 07:01 北京

图片

大家好,我是一安~

Java开发中,经常需要将Java对象转换成Map,或者反过来将Map转换成Java对象。这种转换在很多场景下都非常有用,比如在序列化和反序列化过程中、在数据传输和持久化时、或者在进行对象属性的批量操作时。

本文将介绍几种不同的方法来实现Java对象和Map之间的相互转换,选择哪种方法取决于项目的具体需求和个人偏好。

方法一:使用Spring Framework的ReflectionUtils

Bean转为Map

Person person = new Person(); person.setAge(18); person.setOpenid(""); person.setName("一安"); person.setSubName("公众号"); System.out.println(bean2Map(person)); System.out.println(bean2Map2(person)); public static Map<String, Object> bean2Map(Object object) {     Map<String, Object> 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<String, Object> bean2Map2(Object object) {     Map<String, Object> 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<String, Object> map = new HashMap(); map.put("age", 18); map.put("openid", ""); map.put("name", "一安"); map.put("subName", "公众号"); System.out.println(map2Bean(map, Person.class)); System.out.println(map2Bean2(map, Person.class)); public static <T> T map2Bean(Map<String, Object> map, Class<T> 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> T map2Bean2(Map<String, Object> map, Class<T> 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工具

<dependency>     <groupId>cn.hutool</groupId>     <artifactId>hutool-all</artifactId>     <version>5.8.5</version> </dependency> 

Bean转为Map

Person person = new Person(); person.setAge(18); person.setOpenid(""); person.setName("一安"); person.setSubName("公众号"); Map<String, Object> map = BeanUtil.beanToMap(person); 

Map转为Bean

Map<String, Object> map = new HashMap(); map.put("age", 18); map.put("openid", ""); 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(""); person.setName("一安"); person.setSubName("公众号"); System.out.println(bean2Map(person)); public static Map<String, Object> bean2Map(Object object) {         ObjectMapper objectMapper = new ObjectMapper();         return objectMapper.convertValue(object, new TypeReference<Map<String, Object>>() {         });     } 

Map转为Bean

Map<String, Object> map = new HashMap(); map.put("age", 18); map.put("openid", ""); map.put("name", "一安"); map.put("subName", "公众号"); System.out.println(map2Bean(map, Person.class)); public static <T> T map2Bean(Map<String, Object> map, Class<T> clazz){     ObjectMapper objectMapper = new ObjectMapper();     return objectMapper.convertValue(map, clazz); } 

方法四:使用Apache Commons Lang的BeanUtils

<dependency>     <groupId>org.apache.commons</groupId>     <artifactId>commons-lang3</artifactId>     <version>3.12.0</version>  </dependency> 

Bean转为Map

Person person = new Person(); person.setAge(18); person.setOpenid(""); person.setName("一安"); person.setSubName("公众号"); System.out.println(bean2Map(person)); public static Map<String, String> 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<String, Object> map = new HashMap(); map.put("age", 18); map.put("openid", ""); map.put("name", "一安"); map.put("subName", "公众号"); System.out.println(map2Bean(map, Person.class)); public static <T> T map2Bean(Map<String, ?> map, Class<T> 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工具

<dependency>     <groupId>com.alibaba</groupId>     <artifactId>fastjson</artifactId>     <version>1.2.83</version> </dependency> 

Bean转为Map

Person person = new Person(); person.setAge(18); person.setOpenid(""); person.setName("一安"); person.setSubName("公众号"); System.out.println(JSONObject.parseObject(JSONObject.toJSONString(person))); 

Map转为Bean

Map<String, Object> map = new HashMap(); map.put("age", 18); map.put("openid", ""); map.put("name", "一安"); map.put("subName", "公众号"); System.out.println(JSONObject.parseObject(JSONObject.toJSONString(map), Person.class)); 

如何在Java中实现对象和Map之间的转换

一安未来

致力于Java,大数据;心得交流,技术分享;

154篇原创内容

公众号

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/120274.html

(0)
上一篇 2025-10-30 20:10
下一篇 2025-10-30 20:15

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信