Map对象与实体类Object对象相互转换

文章目录

    • 方法一 json 转换
    • 方法二 BeanUtils转换
    • 方法三 Introspector转换
    • 方法四 reflect 转换

方法一 json 转换

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.1.46.sec01version>
dependency>
// 实体类转json 再转为map
String json = JSONObject.toJSONString(entityObj);
Map map = JSONObject.parseObject(json, Map.class);​​​​​​​
/**
 * Map转换为对象
 * @param paramMap
 * @param cls
 * @return
 */
public static <T> T parseMap2Object(Map<String, Object> paramMap, Class<T> cls) {
    // return JSON.parseObject(JSON.toJSONString(Map), Class cls);
	return JSONObject.parseObject(JSONObject.toJSONString(paramMap), cls);
}
// 实体类转map
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(entity);
JSONObject jsonObject = JSONObject.parseObject(json);

方法二 BeanUtils转换

/**
 * Map转换为对象
 */
BeanUtils.populate(Object bean, Map<String, ? extends Object> properties)
/**
 * Map转换为对象
 */
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
    if (map == null)  
        return null;  

    Object obj = beanClass.newInstance();  

    org.apache.commons.beanutils.BeanUtils.populate(obj, map);  

    return obj;  
}    
 /**
  * 对象转换为Map
  */  
public static Map<?, ?> objectToMap(Object obj) {  
    if(obj == null)  
        return null;   

    return new org.apache.commons.beanutils.BeanMap(obj);  
}  

方法三 Introspector转换

/**
 * Map转换为对象
 */
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
    if (map == null)   
        return null;    

    Object obj = beanClass.newInstance();  

    BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
    for (PropertyDescriptor property : propertyDescriptors) {  
        Method setter = property.getWriteMethod();    
        if (setter != null) {  
            setter.invoke(obj, map.get(property.getName()));   
        }  
    }  

    return obj;  
}    
/**
 * 对象转换为Map
 */  
public static Map<String, Object> objectToMap(Object obj) throws Exception {    
    if(obj == null)  
        return null;      

    Map<String, Object> map = new HashMap<String, Object>();   

    BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
    for (PropertyDescriptor property : propertyDescriptors) {    
        String key = property.getName();    
        if (key.compareToIgnoreCase("class") == 0) {   
            continue;  
        }  
        Method getter = property.getReadMethod();  
        Object value = getter!=null ? getter.invoke(obj) : null;  
        map.put(key, value);  
    }    

    return map;  
} 

方法四 reflect 转换

/**
 * Map转换为对象
 */
public static Object mapToObject(Map<String, 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);    
        field.set(obj, map.get(field.getName()));   
    }   

    return obj;    
}    
/**
 * 对象转换为Map
 */  
public static Map<String, Object> objectToMap(Object obj) throws Exception {    
    if(obj == null){    
        return null;    
    }   

    Map<String, Object> map = new HashMap<String, Object>();    

    Field[] declaredFields = obj.getClass().getDeclaredFields();    
    for (Field field : declaredFields) {    
        field.setAccessible(true);  
        map.put(field.getName(), field.get(obj));  
    }    

    return map;  
}
/**
 * 利用反射将map集合封装成bean对象
 * 
 * @param params
 * @param clazz
 * @return
 */
public static <T> T mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
	Object obj = clazz.newInstance();
	if (map != null && !map.isEmpty() && map.size() > 0) {
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			String propertyName = entry.getKey(); 	// 属性名
			Object value = entry.getValue();		// 属性值
			String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
			Field field = getClassField(clazz, propertyName);	//获取和map的key匹配的属性名称
			if (field == null){
				continue;
			}
			Class<?> fieldTypeClass = field.getType();
			value = convertValType(value, fieldTypeClass);
			try {
				clazz.getMethod(setMethodName, field.getType()).invoke(obj, value);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
	}
	return (T) obj;
}
/**
 * 对象转map
 * @param obj
 * @return
 */
private static Map<String, Object> objToMap(Object obj) {

	Map<String, Object> map = new HashMap<String, Object>();
	Field[] fields = obj.getClass().getDeclaredFields();	// 获取f对象对应类中的所有属性域
	for (int i = 0, len = fields.length; i < len; i++) {
		String varName = fields[i].getName();
		varName = varName.toLowerCase();					// 将key置为小写,默认为对象的属性
		try {
			boolean accessFlag = fields[i].isAccessible();	// 获取原来的访问控制权限
			fields[i].setAccessible(true);					// 修改访问控制权限
			Object o = fields[i].get(obj);					// 获取在对象f中属性fields[i]对应的对象中的变量
			if (o != null){
				map.put(varName, o.toString());
			}
			fields[i].setAccessible(accessFlag);			// 恢复访问控制权限
		} catch (IllegalArgumentException ex) {
			ex.printStackTrace();
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
		}
	}
	return map;
}
/**
 * 根据给定对象类匹配对象中的特定字段
 * @param clazz
 * @param fieldName
 * @return
 */
private static Field getClassField(Class<?> clazz, String fieldName) {
	if (Object.class.getName().equals(clazz.getName())) {
		return null;
	}
	Field[] declaredFields = clazz.getDeclaredFields();
	for (Field field : declaredFields) {
		if (field.getName().equals(fieldName)) {
			return field;
		}
	}
	Class<?> superClass = clazz.getSuperclass();	//如果该类还有父类,将父类对象中的字段也取出
	if (superClass != null) {						//递归获取
		return getClassField(superClass, fieldName);
	}
	return null;
}

/**
 * 将map的value值转为实体类中字段类型匹配的方法
 * @param value
 * @param fieldTypeClass
 * @return
 */
private static Object convertValType(Object value, Class<?> fieldTypeClass) {
	Object retVal = null;
	
	if (Long.class.getName().equals(fieldTypeClass.getName())
			|| long.class.getName().equals(fieldTypeClass.getName())) {
		retVal = Long.parseLong(value.toString());
	} else if (Integer.class.getName().equals(fieldTypeClass.getName())
			|| int.class.getName().equals(fieldTypeClass.getName())) {
		retVal = Integer.parseInt(value.toString());
	} else if (Float.class.getName().equals(fieldTypeClass.getName())
			|| float.class.getName().equals(fieldTypeClass.getName())) {
		retVal = Float.parseFloat(value.toString());
	} else if (Double.class.getName().equals(fieldTypeClass.getName())
			|| double.class.getName().equals(fieldTypeClass.getName())) {
		retVal = Double.parseDouble(value.toString());
	} else {
		retVal = value;
	}
	return retVal;
}

你可能感兴趣的:(后端技术,java)