JSON transformations with collections or maps in an entity class

Source: Internet
Author: User

Content transferred from: http://biyutong.iteye.com/blog/1566708

This content is only convenient for next search.

If it is a general entity:

 Public classStudentImplementsjava.io.serializable{/*** @Fields Serialversionuid:todo*/     Private Static Final LongSerialversionuid = -1943961352036134112l; PrivateString sname; PrivateInteger age;  PublicString Getsname () {returnsname; }     Public voidsetsname (String sname) { This. sname =sname; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }    }

The following methods can be used when you go from string to Java bean:

Jsonobject jobj == (Student) Jsonobject.tobean (jobj,student.  Class);

There is no problem, but if the attribute contains a complex type, when the attribute has a list, Map, ArrayList, custom type, such as list<teacher> teachers, it is not possible. Error: Morphdynabean cannot is cast to con.test ...

If there is a collection in the converted class at Jsonobject.tobean, you can first define map<string, class> classmap = new hashmap<string, class> (); Put the collection name in the class you want to convert in Classmap, like: Classmap.put ("Teachers", Teacher.class), and then add the parameters to Tobean (), like this: Student student= ( Student) Jsonobject.tobean (str, student.class, CLASSMAP);

For example, I define the following types:

Importjava.util.List;/*** * <p>tiltle:analysismodel.java </p>* <p>description: Model class </p>*@authorbiyutong* @date June, 9:37:54 AM *@version: V1.0*/ Public classAnalysismodelImplementsjava.io.serializable{/*** Fixed conditions*/    PrivateList<condition> regularcondtionlist;//    /*** Dynamic Conditions*/    PrivateList<conditionfield>dynamicconfieldlist; /*** Results*/    PrivateList<resultfield>Resultfield; /*** How many data to get before*/    PrivateInteger Topnum; /*** Sequential or random*/    PrivateString Sortflag; /*** Notes*/    PrivateString Remark; /*** Model name*/    PrivateString modelname; /*** The corresponding XML file name*/    PrivateString FileName;  PublicList<condition>getregularcondtionlist () {returnregularcondtionlist; }     Public voidSetregularcondtionlist (list<condition>regularcondtionlist) {         This. regularcondtionlist =regularcondtionlist; }     PublicList<conditionfield>getdynamicconfieldlist () {returndynamicconfieldlist; }     Public voidSetdynamicconfieldlist (list<conditionfield>dynamicconfieldlist) {         This. dynamicconfieldlist =dynamicconfieldlist; }     PublicInteger Gettopnum () {returnTopnum; }     Public voidsettopnum (Integer topnum) { This. Topnum =Topnum; }     PublicString Getsortflag () {returnSortflag; }     Public voidSetsortflag (String sortflag) { This. Sortflag =Sortflag; }     PublicString Getremark () {returnremark; }     Public voidSetremark (String remark) { This. Remark =remark; }         PublicString Getmodelname () {returnmodelname; }     Public voidsetmodelname (String modelname) { This. modelname =modelname; }     PublicList<resultfield>Getresultfield () {returnResultfield; }     Public voidSetresultfield (list<resultfield>Resultfield) {         This. Resultfield =Resultfield; }     PublicString GetFileName () {returnFileName; }     Public voidsetfilename (String fileName) { This. FileName =FileName; }    }

This is what you need to write when converting:

New Hashmap<string, class>(); Classmap.put ("Regularcondtionlist", Condition.  Class); Classmap.put ("Dynamicconfieldlist", Conditionfield. class ); Classmap.put ("Resultfield", Resultfield. class ); Analysismodel Analysismodel= (analysismodel) jsonobject.tobean (str, Analysismodel.  Class, Classmap);

JSON transformations with collections or maps in an entity 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.