java实体类转map
南大白 人气:7java实体类转成map
1.第一种
<!-- 配置gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> Map<String, Object> map = null; String jsonString = JSONUtil.toJsonStr(uploadBaseEntity); Gson gson = new Gson(); map = new HashMap<>(); map = gson.fromJson(jsonString, map.getClass());
2.第二种
/** * 对象转化为Map * * @param obj * @return * @throws Exception */ public static Map<String, String> objectToMap01(Object obj) throws Exception { if (obj == null) { return null; } Map<String, String> map = new HashMap<String, String>(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); map.put(field.getName(), (String) field.get(obj)); } return map; }
java实体类与map集合互转
废话不说,直接上代码!留着用吧...
package com.ddm.message.test; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Java实体类与Map互转 * @author Administrator * */ public class entityMapTransUtils { private static final Logger logger = LoggerFactory.getLogger(entityMapTransUtils.class); /** * Java实体类转Map:方法一 * @param obj * @return */ public static Map<String, Object> entityToMap1(Object obj){ Map<String, Object> map = new HashMap<String, Object>(); Class<?> clazz = obj.getClass(); for(Field field : clazz.getDeclaredFields()){ field.setAccessible(true); String fieldName = field.getName(); Object object = null; try { object = field.get(obj); } catch (IllegalArgumentException | IllegalAccessException e) { logger.info(e.getMessage()); } map.put(fieldName, object); } return map; } /** * Java实体类转Map:方法二 * @param obj * @return */ public static Map<String, Object> entityToMap2(Object obj){ Map<String, Object> map = new HashMap<String, Object>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); // 过滤class属性 if (!key.equals("class")) { // 得到property对应的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); map.put(key, value); } } } catch (Exception e) { logger.info(e.getMessage()); } return map; } /** * Map转实体类:要转换的Map的key跟实体类属性名相同的数据会转过去,不相同的字段会为null * @param clazz * @param map * @return */ public static <T> T mapToEntity1(Class<T> clazz,Map<String, Object> map){ T obj = null; try { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); obj = clazz.newInstance(); // 创建 JavaBean 对象 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); // 给 JavaBean 对象的属性赋值 for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { Object value = map.get(propertyName); Object[] args = new Object[1]; args[0] = value; try { descriptor.getWriteMethod().invoke(obj, args); } catch (InvocationTargetException e) { logger.info(e.getMessage()); } } } } catch (IllegalAccessException e) { logger.info(e.getMessage()); } catch (IntrospectionException e) { logger.info(e.getMessage()); } catch (IllegalArgumentException e) { logger.info(e.getMessage()); } catch (InstantiationException e) { logger.info(e.getMessage()); } return (T)obj; } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容