fastJson和Gson都解析失败:
2023-03-07 本文已影响0人
因为我的心
一、前言:
json数据一部分:https:\/\/oss-cn-hongkong.aliyuncs.com
我看到链接中有反斜杠,去除了一下,不小心去除错了,所以引出下方的错误;
1、fastJson报错:
com.alibaba.fastjson.JSONException: unclosed string : p
图片.png
2、GSON报错:
com.google.gson.stream.MalformedJsonException: Invalid escape sequence at line 1 column 236 path $.securityToken
图片.png
3、源数据
{"accessKeyId":"STS.NSzWpB4888wsbkYnHZQGuxzWXr","accessKeySecret":"2GwaBmbQXWbcWo7TzRFtC9X1izq2e2NThQamPkco4PMQ","expiration":1678274636000,"securityToken":"CAIS6wF1q6Ft5B2yfSjIr5DPHMr22Yx0wLGJaX\/frFoES\/pUlZLzkDz2IHBJenNuAOkcvvQxlGtY5\/0alq9pQppCXlecx3bCARAPo22beIPkl5Gfz95t0e+IewW6Dxr8w7WhAYHQR8\/cffGAck3NkjQJr5LxaTSlWS7OU\/TL8+kFCO4aRQ6ldzFLKc5LLw950q8gOGDWKOymP2yB4AOSLjI15VAg1j0ksP\/im5TBsEeAtjCglL9F97aWC4O\/csxhMK14V9qIx+FsfsLDqnUAtkYQqv4v1vwUp2ec7oDGW0M\/+RyDNPHP4k34ma\/V2lVYGoABewnkI+gBxwFlWkA8BWht\/v0fiby9YsD0nHN82oIdEMZIbrlgNJvT2IQr1kg0tp2F1OqJO7sJ0ompKyl2YS3eX0eM5TkgwxKfxRGbGk1mcmmgRrplrfJahtqgcZPsRtt+CeyfAk1LzxxHkkGBFhr1LjBD9jUZ\/QOkdP6fV1WRMlA=","domain":"https:\/\/xiaomingader-book.oss-cn-hongkong.aliyuncs.com","endPoint":"https:\/\/oss-cn-hongkong.aliyuncs.com","bucket":"xiaoming-book"}
二、解决:
分析:本来数据可以解析的,我不小心替换错了
1、发现数据中的斜杠替换错了
//错误写法:
//var str = result.replace("/", "").trim()
//正确写法:
var str = result.replace("\\", "").trim()
val parseObject = JSON.parseObject(str)
2、发现转义符不用处理,可以直接解析
//fastJson
val parseObject = JSON.parseObject(result)
//GSON
var bean = GsonUtil.GsonToBean<OSSBean>(it, OSSBean::class.java)