java 对象 转换 为 map 对象
public Map<String, Object> bm2map(String id) {
Iqa2Application obj = service.queryIqa2ApplicationById(id);
if (obj == null)
return null;
Map<String, Object> map = new HashMap<>();
List<Field> fieldList = new ArrayList<>() ;
Class tempClass = obj.getClass();
while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass .getDeclaredFields()));
tempClass = tempClass.getSuperclass();
}
for (Field field : fieldList) {
String varName = field.getName();
try {
boolean accessFlag = field.isAccessible();
field.setAccessible(true);
Object o = field.get(obj);
if (o != null){
map.put(varName, o);
}
field.setAccessible(accessFlag);
} catch (IllegalArgumentException | IllegalAccessException ex) {
ex.printStackTrace();
}
}
return map;
}
|