小工具知识网

当前位置: 首页 > 行业知识 > JSON转换异常:Cannot deserialize instance of `java.lang.String` out of START

行业知识

JSON转换异常:Cannot deserialize instance of `java.lang.String` out of START

2026-12-04 12:35:00 来源:互联网转载

问题与解决方案

问题

  • 在日常开发当中我们往往会碰到很多调用第三接口的业务,那么就免不了各种 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”);

上一篇:乙二醇价格深度解析,市场走势及影响因素全面揭秘

下一篇:Yaoiculture下载指南,一键获取应用方法,轻松开启文化之旅