Java 对象转Map,Java Map转对象方法

Java 对象转Map,Java Map转对象方法

import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class UserInfo {
    private String userName;
    private String idNo;
    private String mobile;
    private String sign;

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    private static Map<String, String> objectToMap(Object object) {
        Map<String,String> map = new HashMap<>();
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                map.put(field.getName(), String.valueOf(field.get(object)));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static void mapToObject(Map<String, Object> map, Object obj) throws Exception {
        if (map == null) {
            return;
        }
        Class<?> cls = obj.getClass();
        for (Field field : cls.getDeclaredFields()) {
            String name = field.getName();
            Object value = map.get(name);
            if (value != null) {
                field.setAccessible(true);
                field.set(obj, value);
            }
        }
    }

    public static void main(String[] args) {
        UserInfo userInfo = new UserInfo();
        userInfo.setMobile("19966667777");
        userInfo.setUserName("张三");
        userInfo.setIdNo("463300122545556699");
        //对象转map1={mobile=19966667777, sign=null, userName=张三, idNo=463300122545556699}
        Map map1 = objectToMap(userInfo);
        System.out.println("对象转map1=" + map1);

        UserInfo user1 = new UserInfo();
        try {
            mapToObject(map1,user1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //map1转对象={"idNo":"463300122545556699","mobile":"19966667777","sign":"null","userName":"张三"}
        System.out.println("map1转对象=" + JSON.toJSONString(user1));


        String json = JSON.toJSONString(userInfo);
        Map map2 = JSON.parseObject(json,Map.class);
        //对象转map2={mobile=19966667777, userName=张三, idNo=463300122545556699}
        System.out.println("对象转map2=" + map2);

        String json2 = JSON.toJSONString(map2);
        UserInfo user2 = JSON.parseObject(json2,UserInfo.class);
        //map2转对象={"idNo":"463300122545556699","mobile":"19966667777","userName":"张三"}
        System.out.println("map2转对象=" + JSON.toJSONString(user2));

        Map<String, String> map3 = null;
        try {
            map3 = BeanUtils.describe(userInfo);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        //对象转map3={mobile=19966667777, sign=null, userName=张三, class=class com.example.core.mydemo.scooter.UserInfo, idNo=463300122545556699}
        System.out.println("对象转map3=" + map3);

//        Map map4 = new HashMap();
//        BeanMap map4 = new BeanMap(userInfo);
        UserInfo testObj = new UserInfo();
        try {
            BeanUtils.populate(testObj,map3);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //map3转对象={"mobile":"19966667777","userName":"张三","idNo":"463300122545556699"}
        System.out.println("map3转对象=" + JSON.toJSON(testObj));

    }
}

需要引入的jar包文件:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
		<dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.1</version>
        </dependency>

 

posted on 2023-09-25 23:37  oktokeep  阅读(683)  评论(0编辑  收藏  举报