Mutual conversion between Object & Map

Student business object definition: Student

Student student = new Student();
student.setId(1L);
student.setName("令狐冲")
student.setAge(10)

The first method: through Alibaba Fastjson

pom.xml file dependencies

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

Code 

//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);

The second type: through SpringBoot's own Jackso implementation

In general, we introduce MVC, which helps us introduce Jackso dependencies

import dependencies

 <!-- springboot web(MVC)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

Final dependencies:

Code 

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

The third way: through the Apache common Bean tool class

pom.xml file dependencies

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
</dependency>

Code

#使用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);

The fourth type: realized by reflection

Realize Bean to Map through reflection

//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;
}

Realize Map to Bean through reflection

//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;
}

Guess you like

Origin blog.csdn.net/m0_69057918/article/details/132151333