Re: [json-lib-user] JSONObject.toBean() problem
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2006-11-23 17:05:28
|
I'm sorry for the confusion, but the last email is not exactly true.=0AThe = example describes the ability of JSON-lib to create DynaBeans inside a list= =0Abecause it doesn't have any clues of what type of objetcs it must create= ; then=0Aproceeds to exemplify the use of the DynaBeanToBeanMorpher.=0A=0AI= 've tested the code you sent against release 0.9 and it works without a pro= blem,=0Acan you tell me please which version are you using ? =0A =0A-------= ------------------------------------=0AIng. Andres Almiray Jaramillo=0Ahttp= ://jroller.com/page/aalmiray=0A--=0AWhat goes up, must come down. Ask any s= ystem administrator.=0AThere are 10 types of people in the world: Those who= understand binary, and those who don't.=0ATo understand recursion, we must= first understand recursion.=0A=0A----- Original Message ----=0AFrom: Andre= s Almiray <aal...@ya...>=0ATo: =EA=B9=80=EC=9D=91=EC=88=98 <kimos@rol= izen.com>; jso...@li...=0ASent: Thursday, November 2= 3, 2006 10:52:58 AM=0ASubject: Re: [json-lib-user] JSONObject.toBean() prob= lem=0A=0AYou are correct, the docs do not state that the toBean() method ne= eds a =0Ahint of what type of objects are inside the collection, the correc= t code is=0A=0AString json =3D "{'data':[{'name':'Wallace'},{'name':'Grommi= t'}]}";=0AJSONObject obj =3D new JSONObject(json);=0AMap classMap =3D new H= ashMap();=0AclassMap.put( "data", Person.class );=0AMyBean bean =3D (MyBean= ) JSONObject.toBean( obj, MyBean.class, classMap );=0A=0AThank you for spot= ting this problem :)=0A =0A-------------------------------------------=0AIn= g. Andres Almiray Jaramillo=0Ahttp://jroller.com/page/aalmiray=0A--=0AWhat = goes up, must come down. Ask any system administrator.=0AThere are 10 types= of people in the world: Those who understand binary, and those who don't.= =0ATo understand recursion, we must first understand recursion.=0A=0A----- = Original Message ----=0AFrom: =EA=B9=80=EC=9D=91=EC=88=98 <ki...@ro...= m>=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 MyBean{=0Aprivate List data;=0A=0Apublic List getData() {=0Areturn d= ata;=0A}=0A=0Apublic void setData(List data) {=0Athis.data =3D data;=0A}=0A= }=0A=0Apublic static class Person{=0Aprivate String name;=0A=0Apublic Strin= g getName() {=0Areturn=0A name;=0A}=0A=0Apublic void setName(String name) {= =0Athis.name =3D name;=0A}=0A}=0A=0Apublic static void testTest() throws Ex= ception=0A{=0AString json =3D "{'data':[{'name':'Wallace'},{'name':'Grommit= '}]}";=0AJSONObject 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 "main" net.sf.json.JSONException:=0Ajava.lang.NoSuchMethodExcept= ion: Unknown property 'name'=0Aat net.sf.json.JSONObject.toBean(JSONObject.= java:439)=0Aat net.sf.json.JSONArray.toList(JSONArray.java:349)=0Aat net.sf= .json.JSONObject.toBean(JSONObject.java:376)=0Aat net.sf.json.JSONObject.to= Bean(JSONObject.java:325)=0Aat com.rolizen.test.jsonlib.JsonLibTest.testTes= t(JsonLibTest.java:96)=0Aat=0A com.rolizen.test.jsonlib.JsonLibTest.main(Js= onLibTest.java:21)=0ACaused by: java.lang.NoSuchMethodException: Unknown pr= operty 'name'=0Aat=0Aorg.apache.commons.beanutils.PropertyUtilsBean.setSimp= leProperty(PropertyUtilsBean.java:1741)=0Aat=0Aorg.apache.commons.beanutils= .PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)=0Aat=0Aor= g.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.= java:1677)=0Aat=0Aorg.apache.commons.beanutils.PropertyUtils.setProperty(Pr= opertyUtils.java:559)=0Aat net.sf.json.JSONObject.setProperty(JSONObject.ja= va:484)=0Aat net.sf.json.JSONObject.toBean(JSONObject.java:408)=0A... 5 mor= e=0A------------------------------------------------------------=0A=0AI thi= nk that at the 376 line in the JSONObject.java, invoking=0AJSONArray.toList= (...) method,=0Athe second parameter, 'beanClass' should not be passed.=0AI= nstead, null should be passed, I think.=0AThe intended class of list item i= s 'Person', but=0A 'beanClass' is 'MyBean',=0Aso PropertyUtils cannot find = 'name' property from 'beanClass'.=0A=0ADoesn't anyone have this problem?=0A= =0ASorry for poor english. :(=0A=0A=0A-------------------------------------= ------------------------------------=0ATake Surveys. Earn Cash. Influence t= he Future of IT=0AJoin SourceForge.net's Techsay panel and you'll get the c= hance to share your=0Aopinions on IT & business topics through brief survey= s - and earn cash=0Ahttp://www.techsay.com/default.php?page=3Djoin.php&p=3D= sourceforge&CID=3DDEVDEV=0A_______________________________________________= =0Ajson-lib-user mailing list=0Aj...@li...=0Ahttps= ://lists.sourceforge.net/lists/listinfo/json-lib-user=0A=0A=0A=0A=0A=0A=09= =09 =0ADo You Yahoo!? =0ALa mejor conexi=C3=B3n a Internet y 2GB extra a t= u correo por $100 al mes. http://net.yahoo.com.mx =0A----------------------= ---------------------------------------------------=0ATake Surveys. Earn Ca= sh. Influence the Future of IT=0AJoin SourceForge.net's Techsay panel and y= ou'll get the chance to share your=0Aopinions on IT & business topics throu= gh brief surveys - and earn cash=0Ahttp://www.techsay.com/default.php?page= =3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV=0A________________________________= _______________=0Ajson-lib-user mailing list=0Aj...@li...urcefo= rge.net=0Ahttps://lists.sourceforge.net/lists/listinfo/json-lib-user=0A=0A= =0A=0A=0A=0A=0A=0A=09=0A=09=0A=09=09=0A____________________________________= _______________________ =0ADo You Yahoo!? =0ALa mejor conexi=C3=B3n a Inter= net y <b >2GB</b> extra a tu correo por $100 al mes. http://net.yahoo.com.m= x =0A |