net.sf.json.JSONException: JSONObject["节点名称"] not found解决方法

当尝试访问JSON对象中不存在的节点时,会抛出net.sf.json.JSONException。避免这种错误的方法是在访问节点前先使用has()方法判断其是否存在。例如,通过jsonObj3.has("节点名称")来检查节点是否存在,如果存在则执行相应逻辑,否则跳过,从而有效防止异常。
摘要由CSDN通过智能技术生成

一开始的时候,我是这样判断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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值