本文介绍了java.lang.IllegalStateException:不是JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试使用SonarQube Web API从SonarQube获取json响应。将 JsonElement 分配到 JsonObject 时,我收到此错误:I am trying to get a json response from SonarQube using the SonarQube web API. While assigning the JsonElement into a JsonObject I am getting this error:java.lang.IllegalStateException: Not a JSON Object 这是我的Java类This is my Java class@Controllerpublic class SonarController { @Autowired JenkinsDataService jenkinsService; @Autowired SonarDataService sonarService; HttpEntity<String> entity; @RequestMapping(value = { "/sonar" }) public void test(HttpServletRequest request, HttpServletResponse response,ModelMap model ) throws ServletException, IOException{ try{ String url= "http://sbstpvmlx818:9000/api/resources?metrics=coverage,sonar&depth=1&metrics=ncloc"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); entity = new HttpEntity<String>("parameters", headers); ResponseEntity<String> sonarResponse = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); JsonElement jelement = new JsonParser().parse(sonarResponse.getBody()); JsonObject jobject = jelement.getAsJsonObject(); } catch(Exception e) { e.printStackTrace(); } }} 这是我的Json回复[{"id":2772,"uuid":"AVeKDMBLuPlc0jzh2Dos","key":"com.testgroup.sara:platform","name":"platform","scope":"PRJ","qualifier":"TRK","date":"2017-03-03T12:04:35+0530","creationDate":"2016-10-03T15:47:53+0530","lname":"platform","version":"1.0","msr":[{"key":"ncloc","val":590737.0,"frmt_val":"590,737"}]},{"id":1,"uuid":"AVeJ89Y3uPlc0jzh2Doq","key":"com.testgroup.tbms:tbms","name":"Tbms","scope":"PRJ","qualifier":"TRK","date":"2017-03-06T12:32:57+0530","creationDate":"2016-10-03T15:20:41+0530","lname":"Tbms","version":"5.0","msr":[{"key":"ncloc","val":427062.0,"frmt_val":"427,062"}]},{"id":6156,"uuid":"AVqJnkVdkCpBdZtQIlHD","key":"com.testgroup.sara.services.xdm:xdmf","name":"sara Services :: XDMF","scope":"PRJ","qualifier":"TRK","date":"2017-03-01T16:59:57+0530","creationDate":"2017-03-01T16:55:35+0530","lname":"sara Services :: XDMF","version":"0.9","description":"sara :: SERVICES","msr":[{"key":"ncloc","val":5798.0,"frmt_val":"5,798"}]}] 这是错误java.lang.IllegalStateException: Not a JSON Object: [{"id":2772,"uuid":"AVeKDMBLuPlc0jzh2Dos","key":"com.testgroup.sara:platform","name":"platform","scope":"PRJ","qualifier":"TRK","date":"2017-03-03T12:04:35+0530","creationDate":"2016-10-03T15:47:53+0530","lname":"platform","version":"1.0","msr":[{"key":"ncloc","val":590737.0,"frmt_val":"590,737"}]},{"id":1,"uuid":"AVeJ89Y3uPlc0jzh2Doq","key":"com.testgroup.tbms:tbms","name":"Tbms","scope":"PRJ","qualifier":"TRK","date":"2017-03-06T12:32:57+0530","creationDate":"2016-10-03T15:20:41+0530","lname":"Tbms","version":"5.0","msr":[{"key":"ncloc","val":427062.0,"frmt_val":"427,062"}]},{"id":6156,"uuid":"AVqJnkVdkCpBdZtQIlHD","key":"com.testgroup.sara.services.xdm:xdmf","name":"sara Services :: XDMF","scope":"PRJ","qualifier":"TRK","date":"2017-03-01T16:59:57+0530","creationDate":"2017-03-01T16:55:35+0530","lname":"sara Services :: XDMF","version":"0.9","description":"sara :: SERVICES","msr":[{"key":"ncloc","val":5798.0,"frmt_val":"5,798"}]}]at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90)at com.test.reldashboard.controller.SonarController.test(SonarController.java:71)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at com.test.reldashboard.filter.UriFilter.doFilter(UriFilter.java:64)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at com.test.reldashboard.filter.SessionFilter.doFilter(SessionFilter.java:43)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:662)推荐答案读作JSONArray读取jsonarrayread as JSONArray to read jsonarray if (jelement instanceof JsonObject) { JsonObject jobject = new JsonObject(jelement .getAsJsonObject()); } else if (jelement instanceof JsonArray) { JsonArray jarray = new JsonArray(jelement.getAsJsonArray()); } 这篇关于java.lang.IllegalStateException:不是JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 04:56