com.alibaba.fastjson.JSONException: not close json text, token : error

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


前言

com.alibaba.fastjson.JSONException: not close json text, token : error
at com.alibaba.fastjson.parser.DefaultJSONParser.close(DefaultJSONParser.java:1533)
at com.alibaba.fastjson.JSON.parse(JSON.java:186)
at com.alibaba.fastjson.JSON.parse(JSON.java:192)
at com.alibaba.fastjson.JSON.parse(JSON.java:148)
at com.picc.controller.RobotConfigController.getDecByStr(RobotConfigController.java:141)
at sun.reflect.GeneratedMethodAccessor79.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)

原因

仔细看代码没有问题

Object parse = JSON.parse(substring);

那么原因就出在传入的字符串身上,我得是客户端调用的,华为 小米 vivo传入的都是正常的JSON字符串,但是苹果手机传入的字符串不一样他会在字符串JSON对象后边追加一个莫名其妙的符号 可能是空格
正常是这样的JSON

{"userName":"展明","userId":"1111"}

不正常的是这种 后边有个小符号 粘贴出来没了 所以推测是空格之类的

{"userName":"展明","userId":"1111"}

com.alibaba.fastjson.JSONException: not close json text, token : error_java

解决

既然知道可能是JSON字符串结构问题 那就简单了,如果确定是空格可以直接调用trim()方法,保险起见我决定截取彻底

int i = str.lastIndexOf("{");
int j = str.lastIndexOf("}");
if(i==-1||j==-1){
br.setErrno(400);
br.setData(str);
br.setErrmsg("转换失败");
return ResponseEntity.ok(br);
}
String substring = str.substring(i, j + 1);
System.out.println("截取字符串:"+substring);

确保参数的情况下截取然后再转换


阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“com.alibaba.fastjson.JSONException: not close json text, token : error” 的相关文章