对象转成map对象
程序员文章站
2022-06-15 10:13:57
...
/**
* 将Object对象里面的属性和值转化成Map对象
*
* @param obj
* @return
* @throws IllegalAccessException
*/
public static <T> Map<String, String> objectToMap(T obj) {
Map<String, String> map = new HashMap<String, String>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = null;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (null != value) {
map.put(fieldName, String.valueOf(value));
}
}
return map;
}