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 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; } Mapmap = new HashMap (); 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 entry : ((Map )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; }