JAVABEAN递归转MAP实现

public class ObjectToMap{
    
    public static Map objectToMap(Object obj){
        try{
            Class type = obj.getClass();
            Map returnMap = new HashMap();
            BeanInfo beanInfo = Introspector.getBeanInfo(type);

            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
            for (int i = 0; i< propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (!propertyName.equals("class")) {
                    Method readMethod = descriptor.getReadMethod();
                    Object result = readMethod.invoke(obj, new Object[0]);
                    if(result == null){
                        continue;
                    }
                    //判断是否为 基础类型 String,Boolean,Byte,Short,Integer,Long,Float,Double
                    //判断是否集合类,COLLECTION,MAP              
                    if(result instanceof String
                            || result instanceof Boolean
                            || result instanceof Byte
                            || result instanceof Short
                            || result instanceof Integer
                            || result instanceof Long
                            || result instanceof Float
                            || result instanceof Double
                            || result instanceof Enum
                            ){
                        if (result != null) {
                            returnMap.put(propertyName, result);
                        }
                    }else if(result instanceof Collection){                        
                        Collection<?> lstObj = arrayToMap((Collection<?>)result);
                        returnMap.put(propertyName, lstObj);
                                
                    }else if(result instanceof Map){
                        Map<Object,Object> lstObj = mapToMap((Map<Object,Object>)result);
                        returnMap.put(propertyName, lstObj);
                    } else {
                        Map mapResult = objectToMap(result);
                        returnMap.put(propertyName, mapResult);
                    }
                    
                }
            }
            return returnMap;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        
    }    
    
    private static Map<Object, Object> mapToMap(Map<Object, Object> orignMap) {
        Map<Object,Object> resultMap = new HashMap<Object,Object>();
        for(Entry<Object, Object> entry:orignMap.entrySet()){
            Object key = entry.getKey();
            Object resultKey = null;
            if(key instanceof Collection){
                resultKey = arrayToMap((Collection)key);
            }else if(key instanceof Map){
                resultKey = mapToMap((Map)key);
            }
            else{
                if(key instanceof String
                        || key instanceof Boolean
                        || key instanceof Byte
                        || key instanceof Short
                        || key instanceof Integer
                        || key instanceof Long
                        || key instanceof Float
                        || key instanceof Double
                        || key instanceof Enum
                        ){
                    if (key != null) {
                        resultKey = key;
                    }
                }else{
                    resultKey = objectToMap(key);
                }                
            }
            

            Object value = entry.getValue();
            Object resultValue = null;
            if(value instanceof Collection){
                resultValue = arrayToMap((Collection)value);
            }else if(value instanceof Map){
                resultValue = mapToMap((Map)value);
            }
            else{
                if(value instanceof String
                        || value instanceof Boolean
                        || value instanceof Byte
                        || value instanceof Short
                        || value instanceof Integer
                        || value instanceof Long
                        || value instanceof Float
                        || value instanceof Double
                        || value instanceof Enum
                        ){
                    if (value != null) {
                        resultValue = value;
                    }
                }else{
                    resultValue = objectToMap(value);
                }                
            }
            
            resultMap.put(resultKey, resultValue);
        }        
        return resultMap;
    }


    private static Collection arrayToMap(Collection lstObj){
        ArrayList arrayList = new ArrayList();
        
        for (Object t : lstObj) {
            if(t instanceof Collection){
                Collection result = arrayToMap((Collection)t);
                arrayList.add(result);
            }else if(t instanceof Map){
                Map result = mapToMap((Map)t);
                arrayList.add(result);
            } else {
                if(t instanceof String
                        || t instanceof Boolean
                        || t instanceof Byte
                        || t instanceof Short
                        || t instanceof Integer
                        || t instanceof Long
                        || t instanceof Float
                        || t instanceof Double
                        || t instanceof Enum
                        ){
                    if (t != null) {
                        arrayList.add(t);
                    }
                }else{
                    Object result = objectToMap(t);
                    arrayList.add(result);    
                }                
            }
        }
        return arrayList;
    }
    
}

 

 

 

/**
 * 将Object对象转换为Map对象
 * @param obj
 * @return
 * @throws Exception
 */
public static Object objectToMap(Object obj) throws Exception {
    if (obj == null) {
        return null;
    }

    Map<String, Object> map = new HashMap<String, Object>();
    if (!isBaseType(obj)) {
        if (obj.getClass().equals(java.util.List.class) || obj.getClass().equals(java.util.ArrayList.class)) {
            List list=new ArrayList();
            for (int i = 0; i < ((List) obj).size(); i++) {
                Object tmp = ((List) obj).get(i);
                if (isBaseType(tmp)) {
                    list.add(tmp);
                } else {
                    list.add(objectToMap(tmp));
                }

            }
            return list;
        } else if(obj.getClass().equals(java.util.Map.class)|| obj.getClass().equals(java.util.LinkedHashMap.class) || obj.getClass().equals(java.util.HashMap.class)){
            for (Map.Entry<String, Object> entry : ((Map<String, Object>)obj).entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                if (isBaseType(entry.getValue())) {
                    map.put(entry.getKey() + "", entry.getValue());
                } else {
                    map.put(entry.getKey() + "", objectToMap(entry.getValue()));
                }
            }
        }else {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                Object tmp = getFieldValueByName(field.getName(), obj);
                if (isBaseType(tmp)) {
                    field.setAccessible(true);
                    map.put(field.getName(), field.get(obj));
                } else {
                    map.put(field.getName(), objectToMap(tmp));

                }
            }
            map.put("class", obj.getClass().getName());
        }
    }else{
        return obj;
    }
    return map;
}

public static boolean isBaseType(Object object) {
    if (object != null) {
        Class className = object.getClass();
        if (className.equals(java.lang.Integer.class) ||
                className.equals(java.lang.Byte.class) ||
                className.equals(java.lang.Long.class) ||
                className.equals(java.lang.Double.class) ||
                className.equals(java.lang.Float.class) ||
                className.equals(java.lang.Character.class) ||
                className.equals(java.lang.Short.class) ||
                className.equals(java.lang.Boolean.class)||
                className.equals(java.math.BigDecimal.class)||
                className.equals(java.lang.String.class)||
                className.equals(java.lang.Integer.class)) {
            return true;
        }
        return false;
    }
    return true;
}

/**
 * 根据属性名获取属性值   *
 */
private static Object getFieldValueByName(String fieldName, Object o) {
    String firstLetter = fieldName.substring(0, 1).toUpperCase();
    String getter = "get" + firstLetter + fieldName.substring(1);
    Object value = null;
    try {
        Method method = o.getClass().getMethod(getter, new Class[]{});
        value = method.invoke(o, new Object[]{});
    } catch (Exception e) {
        getFieldValueByIsName(fieldName, o);
    }
    return value;
}

/**
 * 根据属性名获取属性值   *
 */
private static Object getFieldValueByIsName(String fieldName, Object o) {
    String firstLetter = fieldName.substring(0, 1).toUpperCase();
    String getter = "is" + firstLetter + fieldName.substring(1);
    Object value = null;
    try {
        Method method = o.getClass().getMethod(getter, new Class[]{});
        value = method.invoke(o, new Object[]{});
    } catch (Exception e) {
        getFieldValueByGetName(fieldName, o);
    }
    return value;
}

/**
 * 根据属性名获取属性值   *
 */
private static Object getFieldValueByGetName(String fieldName, Object o) {
    String getter = "get" + fieldName;
    Object value = null;
    try {
        Method method = o.getClass().getMethod(getter, new Class[]{});
        value = method.invoke(o, new Object[]{});
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值