1. 程式人生 > >實體類轉換為map

實體類轉換為map

//利用反射

public static Map<String, Object> objectToMap(Object obj) {
        Map<String, Object> map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                    map.put(field.getName(), field.get(obj));
            }
        } catch (

        Exception e) {
            e.printStackTrace();
        }
        return map;
    }