List for循环遍历报异常:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.XXXXX

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())){
     
          // 产成品ID
          tBxManufLine.setBxProdId(sysProduct.getId());
      }
  }

List for循环遍历报异常:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.XXXXX_第1张图片

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())) {
     
         // 产成品ID
         tBxManufLine.setBxProdId(sysProduct.getId());
     }
}

你可能感兴趣的:(Java,java)