使用fastJson解析json字符串报错
1.报错信息
INFO [com.ceapon.fire.ams.api.DeviceController] - Start activate...
com.alibaba.fastjson.JSONException: expect '[', but string, pos 269, line 1, column 270"[{\"task_id\":\"11111\"},{\"task_id\":\"22222\"},{\"task_id\":\"33333\"},{\"task_id\":\"44444\"},{\"task_id\":\"55555\"},{\"task_id\":\"66666\"},{\"task_id\":\"77777\"},{\"task_id\":\"88888\"},{\"task_id\":\"99999\"},{\"task_id\":\"100000\"},{\"task_id\":\"111111\"}]"
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:720)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:708)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:703)
at com.alibaba.fastjson.JSON.parseArray(JSON.java:607)
at com.ceapon.fire.ams.service.AccountService.playerRedpacketTask(AccountService.java:2305)
at com.ceapon.fire.ams.api.DateController.playerRedpacketTask(DateController.java:728)
at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.ceapon.fire.ams.api.filter.CorsFilter.doFilter(CorsFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
2.提取有用报错:
com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:703)
at com.alibaba.fastjson.JSON.parseArray(JSON.java:607)
at com.ceapon.fire.ams.service.AccountService.playerRedpacketTask(AccountService.java:2305)
3.需要解析字符串
%5B%7B%22task_id%22%3A%2211111%22%7D%2C%7B%22task_id%22%3A%2222222%22%7D%2C%7B%22task_id%22%3A%2233333%22%7D%2C%7B%22task_id%22%3A%2244444%22%7D%2C%7B%22task_id%22%3A%2255555%22%7D%2C%7B%22task_id%22%3A%2266666%22%7D%2C%7B%22task_id%22%3A%2277777%22%7D%2C%7B%22task_id%22%3A%2288888%22%7D%2C%7B%22task_id%22%3A%2299999%22%7D%2C%7B%22task_id%22%3A%22100000%22%7D%2C%7B%22task_id%22%3A%22111111%22%7D%5D
URL解码后:
[{"task_id":"11111"},{"task_id":"22222"},{"task_id":"33333"},{"task_id":"44444"},{"task_id":"55555"},{"task_id":"66666"},{"task_id":"77777"},{"task_id":"88888"},{"task_id":"99999"},{"task_id":"100000"},{"task_id":"111111"}]
4.问题点:
我本地用postman调用接口没有问题,别人远程访问报错
5.划重点
解析json字符串的时候,只有字符串中含有unicode编码的,都要转换为utf-8编码(与自己的服务器编码一致)。然后再解析,因为我用postman,默认utf-8的,所以我本地怎么测试都不能找到问题,查询资料后,发现是这个问题,只需要转换为utf-8的编码,或者让调用接口方,请求时就是utf-8编码也可