今天我在处理前端传过来的json数据,有多个list对象,我需要强转为我要的List<实体类>
Object sysLabelList = body.getValue("sysLabelList");
List<UsKolOtherPlatform> usKolOtherLists = new ArrayList<UsKolOtherPlatform>();
usKolOtherLists = (List<UsKolOtherPlatform>) sysLabelList
处理下面的批量插入时会报错,错误是 java.util.LinkedHashMap cannot be cast to ......
`List<UsKolOtherPlatform> usSysLabelList = new ArrayList<UsKolOtherPlatform>(usKolOtherLists.size());
for (UsKolOtherPlatform usKolOtherPlatform : usKolOtherLists) {
usKolOtherPlatform.setKolId(id);
usSysLabelList.add(usKolOtherPlatform);
}
usKolOtherPlatformMapper.insertKolOtherForeach(usSysLabelList);`
之后我百度才发现,json强转list的时候转坏了,但是可以换个思路,再把list转回json,之后再把json转回list,神奇的事情发生了,遍历成功
//直接用上面的强制数据会报错,因为强制的时候转坏的
//可以把list转成json
String str= JSON.toJSONString(usKolOtherLists);
//再把json转成list
usKolOtherLists = JSONObject.parseArray(str, 实体类.class);