文章來源地址http://www.zghlxwxcb.cn/news/detail-632825.html
問題與解決方案
問題
- 在日常開發(fā)當(dāng)中我們往往會碰到很多調(diào)用第三接口的業(yè)務(wù),那么就免不了各種 JSON 的轉(zhuǎn)換,下面就來談?wù)勎矣龅降倪@個問題
- 意思:就是說JSON解析失敗,用String類型無法序列化Json對象
- 其實給出的提示已經(jīng)很明確了:比如:Cannot deserialize instance of:無法反序列化,比如:through reference chain:指的就是這個參數(shù)的類型有問題,這里我這個 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,然后獲取的時候再轉(zhuǎn)換一下就可以了:JSONObject json=(JSONObject) JSONObject.toJSON(response.getData());
- 如果要獲取里面的某個參數(shù),可以這樣:json.getString(“xxx”);
文章來源:http://www.zghlxwxcb.cn/news/detail-632825.html
到了這里,關(guān)于JSON轉(zhuǎn)換異常:Cannot deserialize instance of `java.lang.String` out of START_OBJECT token的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!