小二上菜
参考来源:https://blog.csdn.net/u013305864/article/details/80016693
/**
* 将List<Object>转换为List<Map<String,Object>>
* @param list
* @return
*/
private List<Map<String,Object>> listTransToMap(List<Object> list){
List<Map<String,Object>> maps=new ArrayList<Map<String,Object>>();
for(Object obj:list){
Class c = obj.getClass();
Field[] f = c.getDeclaredFields();
Map<String,Object> map=new HashMap<String, Object>();
for(Field fie : f){
try {
//反射知识
fie.setAccessible(true);//取消语言访问检查
map.put(fie.getName(), fie.get(obj));//获取私有变量值
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
//获取父类的私有属性
for(Field fie : c.getSuperclass().getDeclaredFields()){
try {
fie.setAccessible(true);//取消语言访问检查
map.put(fie.getName(), fie.get(obj));//获取私有变量值
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
maps.add(map);
}
return maps;
}