Converting JSON data to Java entity classes

Source: Internet
Author: User

Idea: First convert the JSON-formatted data into Jsonobject and then convert the Jsonobject into Java entity classes (where class attributes include list types)

Java Entity classes:

Searchfilter class
1  Public classSearchfilter {2     PrivateString Groupop;3     4     PrivateList<searchrule>rules;5 6      PublicString Getgroupop () {7         returnGroupop;8     }9 Ten      Public voidSetgroupop (String groupop) { One          This. Groupop =Groupop; A     } -  -      PublicList<searchrule>GetRules () { the         returnrules; -     } -  -      Public voidSetRules (list<searchrule>rules) { +          This. Rules =rules; -}
Searchrule class:
1  Public classSearchrule {2     PrivateString field;3     PrivateString op;4     PrivateString data;5      PublicString GetField () {6         returnfield;7     }8      Public voidSetField (String field) {9          This. field =field;Ten     } One      PublicString getop () { A         returnop; -     } -      Public voidsetop (String op) { the          This. Op =op; -     } -      PublicString GetData () { -         returndata; +     } -      Public voidsetData (String data) { +          This. data =data; A     } at  -  -}

Methods for parsing JSON format strings

1  Publicsearchfilter jsontosearchfilter (String filters) {2Searchfilter Searchfilter =NULL;3         Try {4Jsonobject Jsonobject =Jsonobject.fromobject (filters);//Convert a JSON-formatted string into a Jsonobject object5Jsonarray array = Jsonobject.getjsonarray ("Rules"); If the JSON-formatted string contains an array-formatted attribute, it is converted to Jsonarray to facilitate subsequent conversion to the corresponding entity6List<searchrule> rules =NewArraylist<searchrule>();7              for(inti = 0; I < array.size (); i++) {8Jsonobject Object =(jsonobject) array.get (i); Convert data in array by article9Searchrule rule = (searchrule) Jsonobject.tobean (object, Searchrule.class); Convert between objects by using the Jsonobject.tobean () methodTen rules.add (rule); One             } AString Groupop = jsonobject.getstring ("Groupop"); Simple direct fetch of values -Searchfilter =NewSearchfilter (); Assembling a Searchfilter object - Searchfilter.setgroupop (groupop); the searchfilter.setrules (rules); -}Catch(Exception e) { -System.out.println ("filters=" + filters.tostring () + ". JSON converted to entity class error"); - e.printstacktrace (); +         } -         returnSearchfilter; +}

Only the object conversion of the List<searchrule>rule property in the Searchfilter class is done here, because it is an array of Searchrule objects, which needs to be taken out separately for JSON conversion to object, if the filter is directly Searchfilter The entity conversion will be an error. If the object does not contain a collection such as List,array,map, you can convert the object as if it were 8-9 rows.

Test:

1  Public Static voidMain (string[] args) {2Usermgmtcontroller con =NewUsermgmtcontroller ();3String filters= "{\" groupop\ ": \" or\ ", \" Rules\ ": [{\" field\ ": \" realname\ ", \" op\ ": \" Eq\ "," +4"\" data\ ": \" 1234\ "},{\" field\ ": \" citycode\ ", \" op\ ": \" ne\ ", \" data\ ": \" 5678\ "}]}";5Searchfilter Searchfilter =Con.jsontosearchfilter (filters);6System.out.println ("gop=" +searchfilter.getgroupop () + "rules.size=" +searchfilter.getrules (). Size ());7}
Results: Gop=or rules.size=2




Converting JSON data to Java entity classes

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.