Original Blog Address: http://blog.csdn.net/harrison2010/article/details/43700991
1 Way One2 /** 3 * Creates a jsondynabean from a jsonobject.4 */ 5 Public StaticObject Tobean (jsonobject jsonobject)6 The returned data type is obviously not our usual data type.7 8 Mode two9 /** Ten * Creates a bean from a jsonobject, with a specific target class.<br> One */ A Public StaticObject Tobean (jsonobject jsonobject, Class beanclass) - - Way Three (common) the /** - * Creates a bean from a jsonobject, with a specific target class.<br> - * If beanclass is null, the This method would return a graph of Dynabeans. any - * attribute that's a jsonobject and matches a key in the Classmap would be + * converted to that target class.<br> - * The CLASSMAP has the following conventions: + * <ul> A * <li>every key must is an string.</li> at * <li>every value must be a class.</li> - * <li>a Key may A regular expression.</li> - * </ul> - */ - Public StaticObject Tobean (jsonobject jsonobject, Class beanclass, Map classmap) - in - Mode four to /** + * Creates a bean from a jsonobject, with the specific configuration. - */ the Public StaticObject Tobean (jsonobject jsonobject, Jsonconfig jsonconfig) * Mode 2 is actually called mode four, it seems that the Jsonconfig object is very important, determines the last type of data returned, of course, far from this. $ Mode 3 is also finally called the way 4Panax Notoginseng - the mode Five (common) + /** A * Creates a bean from a jsonobject, with the specific configuration. the */ + Public StaticObject Tobean (Jsonobject jsonobject, Object root, Jsonconfig jsonconfig) - directly to the existing object processing, the JSON data is written to the existing object. $ $ the more common way three with the way five - Example: Then the bean to JSON code - the //two JSON to object - WuyiJsonobject Jsonobject =Jsonobject.fromobject (returnstring); theObject Returnobject =NULL; - //Method One Class+map config the way three WuMap config =NewHashMap (); -Config.put ("Addresses", Address.class); AboutConfig.put ("sametest", person.class); $Returnobject = Jsonobject.tobean (Jsonobject, person.class, config); - System.out.println (Returnobject); - - //Method two Object+jsonconfig way five Ap =NewPerson (); +Jsonconfig JC =NewJsonconfig (); the jc.setclassmap (config); -Jc.setnewbeaninstancestrategy (NewNewbeaninstancestrategy () { $ @Override the PublicObject newinstance (Class target, jsonobject source) the throwsinstantiationexception, Illegalaccessexception, the SecurityException, Nosuchmethodexception, invocationtargetexception { the if(Target! =NULL ){ - if(Target.getname (). Equals (Map.class. GetName ())) { in return NewHashMap (); the } the returnNewBeanInstanceStrategy.DEFAULT.newInstance (target, source); About } the the return NULL; the } + }); - Jsonobject.tobean (Jsonobject, p, JC); theSystem.out.println (P.getaddresses (). Get (0). GetAddress ());
JSON to Bean (Jsonobject Class)