反序列化异常

复杂对象序列化为json字符串之后,反序列化的时候异常,明明类型是对的,但是就是转换失败。比如:

fastjson的时候提示:

fastjson.JSONException: syntax error, expect {, actual string

jackson提示

MismatchedInputException: Cannot construct instance of `java.util.LinkedHash

解决方法

使用TypeReference(jackson和fastjson都有这个对象,包结构不同)
.> 在TypeReference的<> 中放转换的类型

Map<String, List<User>> mao=JSON.parseObject(staticRedisDao.getStr(CACHE_SYS_PARAM_MAP), new TypeReference<Map<String, List<User>>>() {}) ;

或者

Map<String, List<User>> mao=objectMapper.readValue(staticRedisDao.getStr(CACHE_SYS_PARAM_MAP), new TypeReference<Map<String, List<User>>>() {}) ;

相关文章:

  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2021-08-09
  • 2022-02-28
  • 2022-12-23
  • 2023-03-12
  • 2020-07-18
猜你喜欢
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
相关资源
相似解决方案