在这里插入图片描述

问题与解决方案

问题

  • 在日常开发当中我们往往会碰到很多调用第三接口的业务,那么就免不了各种 JSON 的转换,下面就来谈谈我遇到的这个问题
  • 意思:就是说JSON解析失败,用String类型无法序列化Json对象
  • 其实给出的提示已经很明确了:比如:Cannot deserialize instance of:无法反序列化,比如:through reference chain:指的就是这个参数的类型有问题,这里我这个 data 是一个 String 类型,但是三方返回的格式是这样的:{laoliu:xxxx}
HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 24] (through reference chain: xxxResponse["data"])

解决方案

  • 于是乎:把这个字段改成了:Object,然后获取的时候再转换一下就可以了:JSONObject json=(JSONObject) JSONObject.toJSON(response.getData());
  • 如果要获取里面的某个参数,可以这样:json.getString(“xxx”);
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐