JSON to Bean (Jsonobject Class)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.