List for循环报异常:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.XXXXX.XXXXX
List
<MProductEx> sysProducts
=(List
<MProductEx>) resultBean
.getData();
for (MProductEx sysProduct
: sysProducts
) {
if(sysProduct
.getGroupNo().equals(tBxManufLine
.getGroupNo())){
tBxManufLine
.setBxProdId(sysProduct
.getId());
}
}
1.报错原因:今天从前端传到JAVA后端一组对象数组,后端能接收到,也能打印出来,但是遍历取出单个对象的值得时候就报错:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to xxx(实体类) 2.解决方法: 将数据转成json字符串,将json转成需要的对象
import com
.alibaba
.fastjson
.JSON
;
List
<MProductEx> sysProducts
=(List
<MProductEx>) resultBean
.getData();
for (int i
= 0; i
< sysProducts
.size(); i
++) {
MProductEx sysProduct
=JSON
.parseObject(JSON
.toJSONString(sysProducts
.get(i
)),MProductEx
.class);
if (sysProduct
.getGroupNo().equals(tBxManufLine
.getGroupNo())) {
tBxManufLine
.setBxProdId(sysProduct
.getId());
}
}
转载请注明原文地址: https://lol.8miu.com/read-31347.html