net.sf.json.JSONException: JSONObject["data"] is not a JSONArray 错误解决办法

错误:JSONObject[“data”] is not a JSONArray
JSONObject[“data”] 不是一个 JSONArray

原因
先从前端获取JSONObject 里的数据,再获取JSONObject 里的data数组,这里会存在一个问题,data如果没有值就会报上面的错误。

JSONObject json = AcceptUtil.acceptFile(req);
JSONArray data = json.getJSONArray("data");

解决办法
使用if(0!=json.get(“data”).toString().length())进行判断,当data数组的长度不为0时,再去获取data数组

if(0!=json.get("data").toString().length()){
	JSONArray data = json.getJSONArray("data");
}

思路
先在控制台打印json数据:
json:{“data”:" “,“type”:1}
发现data里面没有值,就想能否去判断一下,当data里面有值时,再去获取data里的数据。没有值是不是意味着data为空呢,就去判断data是否为null,然后测试发现还是出现了一样的问题。突然,这个时候好像被一棍子敲醒了,null与” "是不一样的,这时才想到上面的解决办法,先将json.get(“data”)转成字符串类型,再获取长度,判断是不是为0。

补充
之前遇到了一个类似的问题:
net.sf.json.JSONException: JSONObject[“名称”] not found
这个的问题的解决办法前几天有写,在这篇博客有详解。

报错信息
net.sf.json.JSONException: JSONObject[“data”] is not a JSONArray.
at net.sf.json.JSONObject.getJSONArray(JSONObject.java:1306)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
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 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at filter.EncodeFilter.doFilter(EncodeFilter.java:37)
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:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
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:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol A b s t r a c t C o n n e c t i o n H a n d l e r . p r o c e s s ( A b s t r a c t P r o t o c o l . j a v a : 637 ) a t o r g . a p a c h e . t o m c a t . u t i l . n e t . A p r E n d p o i n t AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.AprEndpoint AbstractConnectionHandler.process(AbstractProtocol.java:637)atorg.apache.tomcat.util.net.AprEndpointSocketProcessor.doRun(AprEndpoint.java:2536)
at org.apache.tomcat.util.net.AprEndpoint S o c k e t P r o c e s s o r . r u n ( A p r E n d p o i n t . j a v a : 2525 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r . r u n W o r k e r ( T h r e a d P o o l E x e c u t o r . j a v a : 1145 ) a t j a v a . u t i l . c o n c u r r e n t . T h r e a d P o o l E x e c u t o r SocketProcessor.run(AprEndpoint.java:2525) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor SocketProcessor.run(AprEndpoint.java:2525)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)

你可能感兴趣的:(java)