JSONObject.toBean

前端之家收集整理的这篇文章主要介绍了JSONObject.toBean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

异常:

net.sf.json.JSONException:java.lang.NoSuchMethodException:Unknownproperty'success'onclass'classcom.leagsoft.restservice.common.bean.Message'
	atnet.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:45)
	atnet.sf.json.JSONObject.setProperty(JSONObject.java:1473)
	atnet.sf.json.JSONObject.toBean(JSONObject.java:474)
	atnet.sf.json.JSONObject.toBean(JSONObject.java:249)
	atcom.leagsoft.restservice.json.JsonTest.testJson2(JsonTest.java:20)
	atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
	atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)
	atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)
	atjava.lang.reflect.Method.invoke(UnknownSource)
	atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	atorg.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	atorg.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	atorg.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	atorg.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	atorg.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	atorg.junit.runners.ParentRunner.run(ParentRunner.java:309)
	atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Causedby:java.lang.NoSuchMethodException:Unknownproperty'success'onclass'classcom.leagsoft.restservice.common.bean.Message'
	atorg.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2123)
	atorg.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:890)
	atnet.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:43)
	...27more

javaBean:

packagecom.leagsoft.restservice.common.bean;

importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlRootElement;

importcom.leagsoft.restservice.util.MessageUtil;

/**
*
*@authorHeweipo
*
*/
@XmlRootElement(name="message")//标注类名为XML根节点
@XmlAccessorType(XmlAccessType.FIELD)//表示将所有域作为XML节点
publicclassMessage{
	
	/**
	*信息获取的状态码
	*/
	privateintstatus;
	/**
	*信息获取提示消息
	*/
	privateStringmsg;
	
	publicStringgetMsg(){
		returnmsg;
	}

	publicvoidsetMsg(Stringmsg){
		this.msg=msg;
	}

	publicintgetStatus(){
		returnstatus;
	}

	publicvoidsetStatus(intstatus){
		this.status=status;
	}
}

测试程序:

publicclassJsonTest{
	
	@Test
	publicvoidtestJson2(){
		Stringjson="{'message':{'status':6000,'msg':'未知异常','success':'true'}}";
		Messagemessage=(Message)JSONObject.toBean(JSONObject.fromObject(json).getJSONObject("message"),Message.class);
		System.out.println(message.getStatus());
	}
	
}

结果就报了以上的错误

java.lang.NoSuchMethodException: Unknown property XXXX

具体解决,如下代码

publicclassJsonTest{
	
	@Test
	publicvoidtestJson2(){
		Stringjson="{'message':{'status':6000,'success':'true'}}";
		JsonConfigconfig=newJsonConfig();
		config.setJavaPropertyFilter(newPropertyFilter(){

			publicbooleanapply(ObjectparamObject1,StringparamString,ObjectparamObject2){
				if(paramString.equals("status")||paramString.equals("msg")){
					returnfalse;
				}
				returntrue;
			}});
		Messagemessage=(Message)JSONObject.toBean(JSONObject.fromObject(JSONObject.fromObject(json).getString("message"),config),Message.class);
		
		System.out.println(message.getStatus());
	}
	
}

总结:

config配置项的作用就是过滤属性,出现这个Exception的原因有两点:

1)Json字符串属性过多,没有对应上javaBean的属性,所以程序会抛异常没有对应的属性。(使用gson不会出错)。

2)使用JsonConfig配置项过滤掉多余的属性。注意,是在字符串转为JSONObject的时候过滤。

3)JSONObject.toBean可以接收三个参数,这个接收三个参数的方法不要使用,会报错,而要使用接收两个参数的方法,另外,属性的过滤一定是在转为JSONObject的时候,而不是转Bean的时候。

4)JSONObject.fromObject(JSONObject.fromObject(result).getString("message"),config) 字符串转为JSONObject的时候过滤

5)(Message)JSONObject.toBean(JSONObject.fromObject(JSONObject.fromObject(result).getString("message"),Message.class); 不能使用(Message)JSONObject.toBean(JSONObject.fromObject(JSONObject.fromObject(result).getString("message"),Message.class,config);

否则又报错:

类转换异常

原文链接:https://www.f2er.com/json/289919.html

猜你在找的Json相关文章