java.util.LinkedHashMap cannot be cast to

简介: java.util.LinkedHashMap cannot be cast to

异常描述及复现


FdcpRes对象里有一个List属性。首先把FdcpRes对象变为JSON字符串,然后在把JSON字符串变为FdcpRes对象,此时再获取List属性并且强转,就会报错。

报错为:java.util.LinkedHashMap cannot be cast to


9.png


解决办法


从json字符串转换为FdcpRes的对象中已经没有了泛型,所以可以把这个FdcpRes里的data字段变为字符串重新反序列化


package json2objerror;
import untils.JsonUtils;
import java.util.ArrayList;
import java.util.List;
/**
 * @author chaird
 * @create 2022-04-17 13:11
 */
public class MainOk {
  public static void main(String[] args) {
    FdcpRes res = buildRes();
    // 创建JSON字符串
    String s = JsonUtils.objectToJson(res);
    FdcpRes fdcpRes = JsonUtils.jsonToPojo(s, FdcpRes.class);
    //把List对象里重新变为Json字符串,再重新把Json字符串变为List对象就Ok了
    s = JsonUtils.objectToJson(fdcpRes.getData());
    List<Ecodata> data = JsonUtils.jsonToList(s, Ecodata.class);
    for (Ecodata datum : data) {
      System.out.println(datum);
    }
  }
  public static FdcpRes buildRes(){
    // 创建两个对象
    List<Ecodata> ecodataList = new ArrayList<>();
    Ecodata e1 = new Ecodata();
    e1.setId(1);
    e1.setValue(1D);
    ecodataList.add(e1);
    Ecodata e2 = new Ecodata();
    e2.setId(2);
    e2.setValue(2D);
    ecodataList.add(e2);
    // 封装结果
    FdcpRes res = new FdcpRes();
    res.setCode(200);
    res.setData(ecodataList);
    return res;
  }
}


10.png


源码下载


https://gitee.com/cbeann/Demooo/tree/master/java-demoo/src/main/java/json2objerror

目录
相关文章
|
7月前
|
Java Spring
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
【Java异常】java.lang.ClassCastException: java.io.NotSerializableException cannot be cast to java.lang.S
35 0
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
|
6天前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
78 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
6天前
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘quanZiController‘ method
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘quanZiController‘ method
17 0
|
6天前
|
网络安全
ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi
11 1
|
7月前
|
Java 关系型数据库 MySQL
【Java异常】java.sql.SQLExcetion:Cannot convert value “0000-00-00 00:00:00” from column 9 to TIMESTAMP
【Java异常】java.sql.SQLExcetion:Cannot convert value “0000-00-00 00:00:00” from column 9 to TIMESTAMP
47 0
|
6天前
|
Java
【Java报错】java.lang.ClassCastException: xxxClass cannot be cast to java.lang.Comparable 问题重现+解决
【Java报错】java.lang.ClassCastException: xxxClass cannot be cast to java.lang.Comparable 问题重现+解决
70 0
|
6月前
|
SQL 关系型数据库 MySQL
mysql异常java.math.BigInteger cannot be cast to java.lang.Long
mysql异常java.math.BigInteger cannot be cast to java.lang.Long
|
7月前
|
XML 数据格式
解决 Cannot convert value of type ‘java.lang.String‘ to required type ‘java.sql.Driver‘ for property ‘
解决 Cannot convert value of type ‘java.lang.String‘ to required type ‘java.sql.Driver‘ for property ‘
134 0
|
7月前
|
Java
【Java异常】java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx‘ method
【Java异常】java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘xxx‘ method
64 0