Map转实体类
实体类 = JSONObject.parseObject(JSON.toJSONString(map),实体类.class)
Map的key转list
map.keySet().stream().collect(Collecctor.toList())
Map转javabean 利用java反射机制实现List<Map<String, Object>>转化为List
List<Map<String, Object>> list1 = new ArrayList<>();
JSONArray solicitationList = JSONArray.fromObject(list);
Object[] solicitationArr = solicitationList.toArray();
for (int p = 0; p < solicitationArr.length; p++) {
Map<String, Object> map = new HashMap<>();
JSONObject jsonSolicitation = JSONObject.fromObject(solicitationArr[p]);
map.put("workTypeName", jsonSolicitation.get("name").toString());
map.put("firstTrainQuantity", Long.valueOf(jsonSolicitation.get("firstNum").toString()));
map.put("resumeTrainQuantity", Long.valueOf(jsonSolicitation.get("secondNum").toString()));
map.put("againTrainQuantity", Long.valueOf(jsonSolicitation.get("againNum").toString()));
list1.add(map);
}
List<ReportedPlan> list2 = null;
// 对象字段名称
String fieldname = "";
// 对象方法名称
String methodname = "";
// 对象方法需要赋的值
Object methodsetvalue = "";
try {
list2 = new ArrayList<ReportedPlan>();
Field fields[] = ReportedPlan.class.getDeclaredFields();
for (Map<String, Object> mapdata : list1) {
ReportedPlan reportedPlan = ReportedPlan.class.newInstance();
for (Field field : fields) {
if (null != field) {
// 全部转化为大写
//String dbfieldname = field.getName().toUpperCase();
// 获取字段名称
fieldname = field.getName();
// 拼接set方法
methodname = "set" + StringUtils.capitalize(fieldname);
// 获取data里的对应值
methodsetvalue = mapdata.get(field.getName());
if(methodsetvalue != null ){
Method m = ReportedPlan.class.getDeclaredMethod(methodname, field.getType());
m.invoke(reportedPlan, methodsetvalue);
}else {
continue;
}
// 赋值给字段
}
}
list2.add(reportedPlan);
}
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}