一开始的时候,我是这样判断json节点是否为空的。
String response="xxxxxx";//Java调用接口返回的json数据
JSONObject jsonObj = JSONObject.fromObject(response);//通过解析数据,获取节点
if(null!=jsonObj3.get("tmplDesc")){
designDescription.setTmplDesc(jsonObj3.get("tmplDesc").toString());//如果不为空,则存储进去实体类做记录。designDescription是一个实体类
} else {
designDescription.setTmplDesc("");
}
但是,这样一旦遇到如果没有该节点的时候,就会报错。
net.sf.json.JSONException: JSONObject["你的节点名称"] not found.
at net.sf.json.JSONObject.getString(JSONObject.java:2092)
at com.css.eshop.services.impl.OrderServiceImpl.getSolitaireAndCTCoInfo(OrderServiceImpl.java:6943)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.sp