Re: [json-lib-user] toBean
Brought to you by:
aalmiray
From: Andres A. <aal...@ya...> - 2006-09-06 16:20:15
|
Hmmm, you are right I should elaborate more on this feature, the docs will be updated in a couple of days, but in the meantime here is the scoop: Supouse you have a bean with a List attribute and a Person bean like class MyBean{ private List data; // getters & setters } class Person{ private String name; // getters & setters } With the following JSON String "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}" how do you create populate the 'data' aatribute if you have no idea which type of object must bea created ? String json = "{'data':[{'name':'Wallace'},{'name':'Grommit'}]}"; MyBean bean = JSONObject.toBean( json, MyBean.class ); This yields a MyBean instance that has DynaBeans inside the 'data' attribute', so now comes the part of postprocessing, this can be done with an Iterator Morpher dynaMorpher = new DynaBeanToBeanMorpher( Person.class, JSONUtils.getMorpherRegistry() ); morpherRegistry.registerMorpher( dynaMorpher ); List output = new ArrayList(); for( Iterator i = bean.getData().iterator(); i.hasNext(); ){ output.add( morpherRegistry.morph( Person.class, i.next() ) ); } bean.setData( output ); Regards. --- Fogas László <las...@in...> escribió: > Hi, > > In the project webpage there is so few information > about how to use toBean > method with lists.. > > The second case is similar and it happens when the > target bean has a > Collection (List) as a property and it must contain > other beans. In this > case there is no way to provide hints for class > conversion. The only > possible solution is to postprocess the collection > transforming each > DynaBean into an specific bean. > > To ease the postprocessing scenarios, Json-lib has a > Morpher capable of > transforming a DynaBean into an specific bean, > <http://json-lib.sourceforge.net/apidocs/net/sf/json/util/DynaBeanToBeanMorp > her.html> DynaBeanToBeanMorpher > > Could you give me some samples? > > Regards, Lazlo > ------------------------------------------- Ing. Andres Almiray Jaramillo http://jroller.com/page/aalmiray -- What goes up, must come down. Ask any system administrator. There are 10 types of people in the world: Those who understand binary, and those who don't. To understand recursion, we must first understand recursion. ___________________________________________________________ Do You Yahoo!? La mejor conexión a Internet y <b >2GB</b> extra a tu correo por $100 al mes. http://net.yahoo.com.mx |