Re: [json-lib-user] JSONObject.toBean() problem
Brought to you by:
aalmiray
|
From: Andres A. <aal...@ya...> - 2006-11-23 16:53:07
|
You are correct, the docs do not state that the toBean() method needs a =0A=
hint of what type of objects are inside the collection, the correct code is=
=0A=0AString json =3D "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}";=
=0AJSONObject obj =3D new JSONObject(json);=0AMap classMap =3D new HashMap(=
);=0AclassMap.put( "data", Person.class );=0AMyBean bean =3D (MyBean) JSONO=
bject.toBean( obj, MyBean.class, classMap );=0A=0AThank you for spotting th=
is problem :)=0A =0A-------------------------------------------=0AIng. Andr=
es Almiray Jaramillo=0Ahttp://jroller.com/page/aalmiray=0A--=0AWhat goes up=
, must come down. Ask any system administrator.=0AThere are 10 types of peo=
ple in the world: Those who understand binary, and those who don't.=0ATo un=
derstand recursion, we must first understand recursion.=0A=0A----- Original=
Message ----=0AFrom: =EA=B9=80=EC=9D=91=EC=88=98 <ki...@ro...>=0ATo:=
jso...@li...=0ASent: Thursday, November 23, 2006 5:=
23:56 AM=0ASubject: [json-lib-user] JSONObject.toBean() problem=0A=0AHi all=
.=0A=0AI've run the code in the 'Getting Started' document.=0A=0A----------=
--------------------------------------------------=0Apublic static class My=
Bean{=0Aprivate List data;=0A=0Apublic List getData() {=0Areturn data;=0A}=
=0A=0Apublic void setData(List data) {=0Athis.data =3D data;=0A}=0A}=0A=0Ap=
ublic static class Person{=0Aprivate String name;=0A=0Apublic String getNam=
e() {=0Areturn name;=0A}=0A=0Apublic void setName(String name) {=0Athis.nam=
e =3D name;=0A}=0A}=0A=0Apublic static void testTest() throws Exception=0A{=
=0AString json =3D "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}";=0AJS=
ONObject obj =3D new JSONObject(json);=0AMyBean bean =3D (MyBean)JSONObject=
.toBean( obj, MyBean.class );=0A}=0A---------------------------------------=
---------------------=0A=0AAnd following exception occured.=0A=0A----------=
--------------------------------------------------=0AException in thread "m=
ain" net.sf.json.JSONException:=0Ajava.lang.NoSuchMethodException: Unknown =
property 'name'=0Aat net.sf.json.JSONObject.toBean(JSONObject.java:439)=0Aa=
t net.sf.json.JSONArray.toList(JSONArray.java:349)=0Aat net.sf.json.JSONObj=
ect.toBean(JSONObject.java:376)=0Aat net.sf.json.JSONObject.toBean(JSONObje=
ct.java:325)=0Aat com.rolizen.test.jsonlib.JsonLibTest.testTest(JsonLibTest=
.java:96)=0Aat com.rolizen.test.jsonlib.JsonLibTest.main(JsonLibTest.java:2=
1)=0ACaused by: java.lang.NoSuchMethodException: Unknown property 'name'=0A=
at=0Aorg.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(Prope=
rtyUtilsBean.java:1741)=0Aat=0Aorg.apache.commons.beanutils.PropertyUtilsBe=
an.setNestedProperty(PropertyUtilsBean.java:1648)=0Aat=0Aorg.apache.commons=
.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)=0Aat=
=0Aorg.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.jav=
a:559)=0Aat net.sf.json.JSONObject.setProperty(JSONObject.java:484)=0Aat ne=
t.sf.json.JSONObject.toBean(JSONObject.java:408)=0A... 5 more=0A-----------=
-------------------------------------------------=0A=0AI think that at the =
376 line in the JSONObject.java, invoking=0AJSONArray.toList(...) method,=
=0Athe second parameter, 'beanClass' should not be passed.=0AInstead, null =
should be passed, I think.=0AThe intended class of list item is 'Person', b=
ut 'beanClass' is 'MyBean',=0Aso PropertyUtils cannot find 'name' property =
from 'beanClass'.=0A=0ADoesn't anyone have this problem?=0A=0ASorry for poo=
r english. :(=0A=0A=0A-----------------------------------------------------=
--------------------=0ATake Surveys. Earn Cash. Influence the Future of IT=
=0AJoin SourceForge.net's Techsay panel and you'll get the chance to share =
your=0Aopinions on IT & business topics through brief surveys - and earn ca=
sh=0Ahttp://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=
=3DDEVDEV=0A_______________________________________________=0Ajson-lib-user=
mailing lis...@li...=0Ahttps://lists.sourcef=
orge.net/lists/listinfo/json-lib-user=0A=0A=0A=0A=0A=0A=09=0A=09=0A=09=09=
=0A___________________________________________________________ =0ADo You Ya=
hoo!? =0ALa mejor conexi=C3=B3n a Internet y <b >2GB</b> extra a tu correo =
por $100 al mes. http://net.yahoo.com.mx =0A |