Object & Map 的相互转换

学生业务对象定义:Student

java 复制代码
Student student = new Student();
student.setId(1L);
student.setName("令狐冲")
student.setAge(10)

第一种:通过Alibaba Fastjson实现

pom.xml 文件依赖

java 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.66</version>
</dependency>

代码实现

java 复制代码
//Object转Map
Map map = JSONObject.parseObject(JSONObject.toJSONString(student), Map.class);
Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(student));
//Map转Object
Student s1 = JSON.parseObject(JSON.toJSONString(map), Student.class);
Student s2 = JSONObject.toJavaObject(JSON.toJSONString(map), Student.class);

第二种:通过SpringBoot自带 Jackso实现

一般情况下我们引入MVC,MVC里面帮我们引入了Jackso依赖

导入依赖

XML 复制代码
 <!-- springboot web(MVC)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

最终的依赖:

代码实现

java 复制代码
ObjectMapper mapper = new ObjectMapper();
//对象转map
Map m = mapper.readValue(mapper.writeValueAsString(student), Map.class);
//map转对象
Student s = mapper.readValue(mapper.writeValueAsString(m), Student.class);

第三种:通过Apache common Bean工具类实现

pom.xml文件依赖

XML 复制代码
<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>

代码实现

java 复制代码
#使用org.apache.commons.beanutils.BeanMap进行转换,实现Bean转Map
Map<String, Object> map = new org.apache.commons.beanutils.BeanMap(student);
 
#使用org.apache.commons.beanutils.BeanUtils将map转为对象
BeanUtils.populate(student, map);

第四种: 通过反射实现

通过反射实现Bean 转Map

java 复制代码
//Object转Map
public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    Class<?> clazz = obj.getClass();
    System.out.println(clazz);
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        String fieldName = field.getName();
        Object value = field.get(obj);
        if (value == null){
            value = "";
        }
        map.put(fieldName, value);
    }
    return map;
}

通过反射实现Map转Bean

java 复制代码
//Map转Object
public static Object mapToObject(Map<Object, Object> map, Class<?> beanClass) throws Exception {
    if (map == null)
        return null;
    Object obj = beanClass.newInstance();
    Field[] fields = obj.getClass().getDeclaredFields();
    for (Field field : fields) {
        int mod = field.getModifiers();
        if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
            continue;
        }
        field.setAccessible(true);
        if (map.containsKey(field.getName())) {
            field.set(obj, map.get(field.getName()));
        }
    }
    return obj;
}
相关推荐
木头没有瓜1 分钟前
idea离线安装插件
java·ide·intellij-idea
llwszx11 分钟前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
述雾学java26 分钟前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
保持学习ing26 分钟前
苍穹外卖day3--公共字段填充+新增菜品
java·阿里云·实战·springboot·前后端·外卖项目·阿里云文件存储
77qqqiqi44 分钟前
正则表达式
java·后端·正则表达式
厦门德仔1 小时前
【WPF】WPF(样式)
android·java·wpf
大春儿的试验田1 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
Gappsong8741 小时前
【Linux学习】Linux安装并配置Redis
java·linux·运维·网络安全
hqxstudying1 小时前
Redis为什么是单线程
java·redis
RainbowSea1 小时前
NVM 切换 Node 版本工具的超详细安装说明
java·前端