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

前言

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"}  

在这里插入图片描述

解决

既然知道可能是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);

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

16:29:33.734 DEBUG o.s.web.servlet.DispatcherServlet - GET "/base/chat?question=%E4%BD%A0%E5%A5%BD", parameters={masked} 16:29:33.745 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to com.igruel.Deepseek.DeepSeekController#chat(String) 16:29:40.194 DEBUG o.s.web.servlet.DispatcherServlet - Failed to complete request: com.alibaba.fastjson.JSONException: not close json text, token : { 16:29:40.511 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/base] threw exception [Request processing failed; nested exception is com.alibaba.fastjson.JSONException: not close json text, token : {] with root cause com.alibaba.fastjson.JSONException: not close json text, token : { at com.alibaba.fastjson.parser.DefaultJSONParser.close(DefaultJSONParser.java:1556) at com.alibaba.fastjson.JSON.parseObject(JSON.java:398) at com.alibaba.fastjson.JSON.parseObject(JSON.java:298) at com.alibaba.fastjson.JSON.parseObject(JSON.java:588) at com.igruel.Deepseek.DeepSeekService.chat(DeepSeekService.java:48) at com.igruel.Deepseek.DeepSeekController.chat(DeepSeekController.java:20) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandl
03-17
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值