java中map和对象互转的⽅法总结----MapUtils⼯具类del;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
base.util.StringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import flect.Field;
import flect.Method;
import flect.Modifier;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class MapUtils {
//⽅法001
//json的转换
private static <T> T mapTodata(Map<String ,Object> map, Class<T> obj) throws Exception {
T instance = null;
Map<String, Method> methodMap = new HashMap<>();
//获取类中声明的所有字段
Field[] fields = DeclaredFields();
/
/创建新的实例对象
Map<String, Object> meMap = new HashMap<>();
//利⽤循环
for (int i = 0; i < fields.length; i++) {
//获取字段的名称
String name = fields[i].getName();
//把序列化id筛选掉
if (name.equals("serialVersionUID")) {
continue;
}
// ⾸字母⼤写
String replace = name.substring(0, 1).toUpperCase() + name.substring(1);
Method setMethod = ("set" + replace);
Object strObject = (name);
String str = strObject == null ? "" : String();
if (str != null && !"".equals(str) && StringUtil.isNotEmpty(str) && setMethod!=null) {
meMap.put(name, str);
}
}
try{
//instance = wInstance();
//返回实体类对象
instance= JSON.JSONString(meMap), obj);
//另外⼀种⽅法 ---org.apachemons.beanutils.BeanUtils.populate(obj, map);
return instance;
}catch (Exception e){
object tothrow new Exception("对象转换失败");
}
}
private static Map<String,Object> dataToMap(Object obj){
Map<String, Object> map = JSONObject.JSONString(obj), Map.class);
return map;
}
/
/⽅法002
//遍历对象所有的字段
//Map转Object
public static <T> T mapToObject(Map<Object, Object> map, Class<T> beanClass) throws Exception {
public static <T> T mapToObject(Map<Object, Object> map, Class<T> beanClass) throws Exception { if (map == null)
return null;
T obj = wInstance();
Field[] fields = Class().getDeclaredFields();
for (Field field : fields) {
int mod = Modifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
if (Name())) {
field.set(obj, (Name()));
}
}
return obj;
}
//Object转Map
public static Map<String, Object> getObjectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Class<?> clazz = Class();
System.out.println(clazz);
for (Field field : DeclaredFields()) {
field.setAccessible(true);
String fieldName = Name();
Object value = (obj);
if (value == null){
value = "";
}
map.put(fieldName, value);
}
return map;
}
//⽅法003
//使⽤ com.fasterxml.jackson.databind.ObjectMapper 进⾏转换
public static <T> T mapFoObject (Map<String, Object> map, Class<T> beanClass) throws Exception { if (map == null) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
T obj = vertValue(map, beanClass);
return obj;
}
public static Map<?, ?> objectFoMap (Object obj) {
if (obj == null) {
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
Map<?, ?> mappedObject = vertValue(obj, Map.class);
return mappedObject;
}
//⽅法004
/
/使⽤ org.apachemons.beanutils 进⾏转换
public static <T> T mapToObj(Map<String, Object> map, Class<T> beanClass) throws Exception {
if (map == null) {
return null;
}
T obj = wInstance();
org.apachemons.beanutils.BeanUtils.populate(obj, map);
return obj;
}
public static Map<?, ?> objToMap(Object obj) {
if (obj == null) {
return null;
}
return new org.apachemons.beanutils.BeanMap(obj);
return new org.apachemons.beanutils.BeanMap(obj);
}
//⽅法005
//使⽤ Introspector 进⾏转换
public static <T> T mapToObjecta(Map<String, Object> map, Class<T> beanClass) throws Exception { if (map == null) {
return null;
}
T obj = wInstance();
BeanInfo beanInfo = Class());
PropertyDescriptor[] propertyDescriptors = PropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = WriteMethod();
if (setter != null) {
setter.invoke(obj, (Name()));
}
}
return obj;
}
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 = Class());
PropertyDescriptor[] propertyDescriptors = PropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = Name();
if (keypareToIgnoreCase("class") == 0) {
continue;
}
Method getter = ReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
}
return map;
}
//⽅法006
//使⽤ reflect 进⾏转换
public static <T> T mapToObjectb(Map<String, Object> map, Class<T> beanClass) throws Exception { if (map == null) {
return null;
}
T obj = wInstance();
Field[] fields = Class().getDeclaredFields();
for (Field field : fields) {
int mod = Modifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
field.setAccessible(true);
field.set(obj, (Name()));
}
return obj;
}
public static Map<String, Object> objectToMapb(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
Field[] declaredFields = Class().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.Name(), (obj));
}
}
return map;
}
/**
* 将map的value值转为实体类中字段类型匹配的⽅法
* @param value
* @param fieldTypeClass
* @return
*/
private static Object convertValType(Object value, Class<?> fieldTypeClass) { Object retVal = null;
if (Name().Name())
|| Name().Name())) {
retVal = Long.String());
} else if (Name().Name())
|| Name().Name())) {
retVal = Integer.String());
} else if (Name().Name())
|| Name().Name())) {
retVal = Float.String());
} else if (Name().Name())
|| Name().Name())) {
retVal = Double.String());
} else {
retVal = value;
}
return retVal;
}
}
下划线命名转驼峰命名
/**
* 功能:下划线命名转驼峰命名
* 将下划线替换为空格,将字符串根据空格分割成数组,再将每个单词⾸字母⼤写 * @param para
* @return String
*/
public static String Underline(String para){
if(StringUtil.isNotEmpty(para)){
String res = LowerCase();
StringBuffer stringBuffer = new StringBuffer();
Boolean re= res.lastIndexOf("_") == res.length() - 1;
if(re){
res = res.substring(0, res.length() -1);
}
String a[]=res.split("_");
for(String s:a){
if(stringBuffer.length()==0){
stringBuffer.LowerCase());
}else{
stringBuffer.append(s.substring(0, 1).toUpperCase());
stringBuffer.append(s.substring(1).toLowerCase());
}
}
String();
}else {
return "";
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论