java.util.LinkedHashMap cannot be cast to xxxEntity

报错背景:
ajax从前台传多个list对象的map到controller,要将map中的list取出并把linkedhashmap类型的数组转换成实体类
红框中的是分别对应实体类中的属性,field.get(0)可以看出其值分别对应实体类中的属性,却是以linkedHashMap格式存储
在这里插入图片描述

方法一:
先将linkedHashMap类型的字符串转化成JSON格式的字符串,再转化成实体类
过程:
1、maven添加依赖包

<dependency>
		  <groupId>com.alibaba</groupId>
		  <artifactId>fastjson</artifactId>
		  <version>1.2.38</version>
</dependency>

2、转化

String str = JSON.toJSONString(field.get(0)); //转化成JSON字符串
TbCollectCol temp = JSON.parseObject(str,TbCollectCol.class); //将JSON转化成对象

参考:
【Exception处理】-报错:java.util.LinkedHashMap cannot be cast to ……;errorCode:5010000!

猜你喜欢

转载自blog.csdn.net/weixin_40626699/article/details/86496317
今日推荐