使用反射将JavaBean转为Map

import java.lang.reflect.Field;

private static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = field.get(obj);
map.put(fieldName, value);
}
return map;
}

posted @ 2019-04-26 18:14  生之等待  阅读(102)  评论(0编辑  收藏  举报