Thread: 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 list=0Aj...@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 |
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 |
From: Andres A. <aal...@ya...> - 2006-11-24 15:53:31
|
I have found the problem. This is a bug reported as http://sourceforge.net/= tracker/index.php?func=3Ddetail&aid=3D1592799&group_id=3D171425&atid=3D8579= 28=0Aand has already been fixed on the current development version (0.9-dev= ).=0AWith the example as is, the List will be filled with DynaBeans, if you= want Person objects instead=0Athe following code will work:=0A=0A Str= ing json =3D "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}";=0A JS= ONObject obj =3D new JSONObject( json );=0A Map classMap =3D new HashM= ap();=0A classMap.put( "data", Person.class );=0A MyBean bean =3D= (MyBean) JSONObject.toBean( obj, MyBean.class, classMap );=0A=0ACheers.=0A= =0A-------------------------------------------=0AIng. Andres Almiray Jaram= illo=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 recursi= on, we must first understand recursion.=0A=0A----- Original Message ----=0A= From: =EA=B9=80=EC=9D=91=EC=88=98 <sc...@gm...>=0ATo: Andres Almiray = <aal...@ya...>=0ASent: Thursday, November 23, 2006 10:30:36 PM=0ASubj= ect: Re: [json-lib-user] JSONObject.toBean() problem=0A=0AThank you for rep= ly~ :)=0A=0A=0A=0AI'm using 0.9 version, and JDK 1.5.=0A=0A=0A=0AHave you c= hecked the JSONObject.java source code? =0A=0A=0A=0AJSONObject.java 376 lin= e is=0A=0A> List list =3D JSONArray.toList( (JSONArray) value, beanClass, c= lassMap );=0A=0AThe 'beanClass' parameter is from JSONObject.toBean(...), a= nd it is 'MyBean.class'.=0A=0ABecause 'MyBean.class' doesn't have 'name' pr= operty, the exception was occured.=0A=0A=0A=0ASo, I think that the line sho= uld be like following,=0A=0A> List list =3D JSONArray.toList( (JSONArray) v= alue, null, classMap );=0A=0AThen, the list will have JSONArray instances, = and I will be able to convert it to Person obejct=0A=0Awith DynaBeanToBeanM= orpher.=0A=0A=0A=0AI have another idea. How about processing List like Map?= =0A=0AI think that if the type of a property is Map, the actual class is s= elected from 'classMap' parameter. (Is it right?)=0A=0ALike this, if the ty= pe of a property is List, the class can be selected from extra parameter, '= classList'. =0A=0AThe type of 'classList' may be java.lang.Map<String, Clas= s>. =0A=0AThen I don't have to use DynaBeanToBeanMorpher directly.=0A=0A=0A= =0AThanks for reading~ Good bye~=0A=0A=0A=0A2006/11/24, Andres Almiray <aal= mi...@ya...>:=0AI'm sorry for the confusion, but the last email is not = exactly true.=0AThe example describes the ability of JSON-lib to create Dyn= aBeans inside a list=0A=0Abecause it doesn't have any clues of what type of= objetcs it must create; then=0Aproceeds to exemplify the use of the DynaBe= anToBeanMorpher.=0A=0AI've tested the code you sent against release 0.9 and= it works without a problem,=0A=0Acan you tell me please which version are = you using ? =0A =0A-------------------------------------------=0AIng. Andre= s Almiray Jaramillo=0A=0Ahttp://jroller.com/page/aalmiray=0A--=0AWhat goes = up, must come down. Ask any system administrator.=0AThere are 10 types of p= eople in the world: Those who understand binary, and those who don't.=0ATo = understand=0A recursion, we must first understand recursion.=0A=0A=0A----- = Original Message ----=0AFrom: Andres Almiray <aal...@ya...>=0ATo: =EA= =B9=80=EC=9D=91=EC=88=98 <=0A...@ro...>; jso...@li...urce= forge.net=0ASent: Thursday, November 23, 2006 10:52:58 AM=0A=0ASubject: Re:= [json-lib-user] JSONObject.toBean() problem=0A=0AYou are correct, the docs= do not state that the toBean() method needs a =0A=0Ahint of what type of o= bjects are inside the collection, the correct code is=0A=0AString json =3D = "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}";=0AJSONObject obj =3D ne= w JSONObject(json);=0A=0AMap classMap =3D new HashMap();=0AclassMap.put( "d= ata",=0A Person.class );=0AMyBean bean =3D (MyBean) JSONObject.toBean( obj,= MyBean.class, classMap );=0A=0AThank you for spotting this problem :)=0A = =0A-------------------------------------------=0A=0AIng. Andres Almiray Jar= amillo=0Ahttp://jroller.com/page/aalmiray=0A--=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 re= cursion, we must first understand recursion.=0A=0A=0A----- Original Message= ----=0AFrom: =EA=B9=80=EC=9D=91=EC=88=98 <ki...@ro...>=0ATo: =0Ajson= -li...@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'=0A document.=0A=0A-----------= -------------------------------------------------=0Apublic static class MyB= ean{=0Aprivate List data;=0A=0Apublic List getData() {=0Areturn data;=0A}= =0A=0Apublic void setData(List data) {=0A=0Athis.data =3D data;=0A}=0A}=0A= =0Apublic static class Person{=0Aprivate String name;=0A=0Apublic String ge= tName() {=0Areturn=0A name;=0A}=0A=0Apublic void setName(String name) {=0At= his.name =3D name;=0A}=0A}=0A=0Apublic static void testTest() throws Except= ion=0A=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=0A=0AAnd following exception occured.=0A= =0A------------------------------------------------------------=0AException= in thread "main" net.sf.json.JSONException:=0Ajava.lang.NoSuchMethodExcept= ion: Unknown property 'name'=0A=0Aat net.sf.json.JSONObject.toBean(JSONObje= ct.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= .toBean(JSONObject.java=0A:325)=0Aat com.rolizen.test.jsonlib.JsonLibTest.t= estTest(JsonLibTest.java:96)=0Aat=0A com.rolizen.test.jsonlib.JsonLibTest.m= ain(JsonLibTest.java:21)=0ACaused by: java.lang.NoSuchMethodException: Unkn= own property 'name'=0Aat=0Aorg.apache.commons.beanutils.PropertyUtilsBean.s= etSimpleProperty(PropertyUtilsBean.java=0A:1741)=0Aat=0Aorg.apache.commons.= beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)= =0Aat=0Aorg.apache.commons.beanutils.PropertyUtilsBean.setProperty(Property= UtilsBean.java:1677)=0Aat=0Aorg.apache.commons.beanutils.PropertyUtils.setP= roperty=0A(PropertyUtils.java:559)=0Aat net.sf.json.JSONObject.setProperty(= JSONObject.java:484)=0Aat net.sf.json.JSONObject.toBean(JSONObject.java:408= )=0A... 5 more=0A----------------------------------------------------------= --=0A=0A=0AI think that at the 376 line in the JSONObject.java, invoking=0A= JSONArray.toList(...) method,=0Athe second parameter, 'beanClass' should no= t be passed.=0AInstead, null should be passed, I think.=0AThe intended clas= s of list item is 'Person', but=0A 'beanClass' is 'MyBean',=0Aso PropertyUt= ils cannot find 'name' property from 'beanClass'.=0A=0ADoesn't anyone have = this problem?=0A=0ASorry for poor english. :(=0A=0A=0A---------------------= ----------------------------------------------------=0A=0ATake Surveys. Ear= n Cash. Influence the Future of IT=0AJoin SourceForge.net's Techsay panel a= nd you'll get the chance to share your=0Aopinions on IT & business topics t= hrough brief surveys - and earn cash=0A=0Ahttp://www.techsay.com/default.ph= p?page=3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV=0A__________________________= _____________________=0Ajson-lib-user mailing list=0A=0Ajson-lib-user@lists= .sourceforge.net=0Ahttps://lists.sourceforge.net/lists/listinfo/json-lib-us= er=0A=0A=0A=0A=0A=0A=0A=09=09 =0ADo You Yahoo!? =0ALa mejor conexi=C3=B3n = a Internet y 2GB extra a tu correo por $100 al mes. http://net.yahoo.com.mx= =0A =0A--------------------------------------------------------------------= -----=0ATake Surveys. Earn Cash. Influence the Future of IT=0AJoin SourceFo= rge.net's Techsay panel and you'll get the chance to share your=0Aopinions = on IT & business topics through brief surveys - and earn cash=0A=0Ahttp://w= ww.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV=0A= =0A_______________________________________________=0Ajson-lib-user mailing = list=0Aj...@li...=0A=0Ahttps://lists.sourceforge.n= et/lists/listinfo/json-lib-user=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=09=09 =0ADo = You Yahoo!? =0ALa mejor conexi=C3=B3n a Internet y 2GB extra a tu correo po= r $100 al mes. http://net.yahoo.com.mx =0A=0A=0A=0A=0A---------------------= ----------------------------------------------------=0ATake Surveys. Earn C= ash. 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 thro= ugh brief surveys - and earn cash=0A=0Ahttp://www.techsay.com/default.php?p= age=3Djoin.php&p=3Dsourceforge&CID=3DDEVDEV=0A=0A=0A_______________________= ________________________=0Ajson-lib-user mailing list=0Ajson-lib-user@lists= .sourceforge.net=0A=0Ahttps://lists.sourceforge.net/lists/listinfo/json-lib= -user=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=0A=09=0A=09=0A=09=09=0A__________= _________________________________________________ =0ADo You Yahoo!? =0ALa m= ejor conexi=C3=B3n a Internet y <b >2GB</b> extra a tu correo por $100 al m= es. http://net.yahoo.com.mx =0A |