Solve complex JSON structures with Objectmapper readvalue () and generics

Source: Internet
Author: User

1 ImportCom.dj.fss.vo.MessageListVO;2 Importcom.fasterxml.jackson.annotation.JsonIgnoreProperties;3 Importcom.fasterxml.jackson.core.JsonParseException;4 Importcom.fasterxml.jackson.databind.JsonMappingException;5 ImportCom.fasterxml.jackson.databind.ObjectMapper;6 7 Importjava.io.IOException;8 Importjava.util.List;9 Ten  One /** A * Objectmapper ReadValue () method - * Use generics to solve complex JSON structures - * @JsonIgnoreProperties (Ignoreunknown = True) property not instantiated does not automatically assign a value without this return exception the  * -  * @param<T> -  */ -@JsonIgnoreProperties (Ignoreunknown =true) +  Public classPagebean<textendsObject> { -     PrivateList<messagelistvo>messagelist; +  A     PrivatePageInfo PageInfo; at  -      PublicPagebean.pageinfo GetPageInfo () { -         returnPageInfo; -     } -  -      Public voidSetpageinfo (Pagebean.pageinfo pageinfo) { in          This. PageInfo =PageInfo; -     } to  +      PublicList<messagelistvo>getmessagelist () { -         returnmessagelist; the     } *  $      Public voidSetmessagelist (list<messagelistvo>messagelist) {Panax Notoginseng          This. messagelist =messagelist; -     } the  +@JsonIgnoreProperties (Ignoreunknown =true) A      Public classpageinfo{ the         Private intTotal ; +  -          Public intgettotal () { $             returnTotal ; $         } -  -          Public voidSettotal (intTotal ) { the              This. Total =Total ; -         }Wuyi     } the  -  Wu  -  About     Private StaticString jsonstring = "{\ n" + $"\" messagelist\ ": [\ n" + -"{\ n" + -"\" id\ ": \" 7\ ", \ n" + -"\" sendcompany\ ": \" guangzhou airport \ "\ n" + A"\" recvcompany\ ": \" China Southern Airlines ", \ n" + +"\" messagetype\ ": \" 1001\ ", \ n" + the"\" sendtime\ ": \" 2018-07-12 16:43:06\ "\ n" + -"},\n" + $"{\ n" + the"\" id\ ": \" 4\ ", \ n" + the"\" sendcompany\ ": \" guangzhou airport \ "\ n" + the"\" recvcompany\ ": \" China Southern Airlines ", \ n" + the"\" messagetype\ ": \" 1002\ ", \ n" + -"\" sendtime\ ": \" 2018-07-12 15:49:02\ "\ n" + in"}\n" + the"],\n" + the"\" pageinfo\ ": {\ n" + About"\" pagenum\ ": 1,\n" + the"\" pagesize\ ": 3,\n" + the"\" size\ ": 2,\n" + the"\" startrow\ ": 1,\n" + +"\" endrow\ ": 2,\n" + -"\" total\ ": 2,\n" + the"\" pages\ ": 1,\n" +Bayi"\" list\ ": null,\n" + the"\" prepage\ ": 0,\n" + the"\" nextpage\ ": 0,\n" + -"\" isfirstpage\ ": true,\n" + -"\" islastpage\ ": true,\n" + the"\" haspreviouspage\ ": false,\n" + the"\" hasnextpage\ ": false,\n" + the"\" navigatepages\ ": 8,\n" + the"\" navigatepagenums\ ": [\ n" + -"1\n" + the"],\n" + the"\" navigatefirstpage\ ": 1,\n" + the"\" navigatelastpage\ ": 1,\n" +94"\" firstpage\ ": 1,\n" + the"\" lastpage\ ": 1\n" + the"}\n" + the"    }";98  About      Public Static voidMain (string[] args)throwsjsonparseexception, Jsonmappingexception, IOException { - 101 102 103Objectmapper mapper =NewObjectmapper ();104pagebean<messagelistvo> Strpagebean = Mapper.readvalue (jsonstring, Pagebean.class); theSystem.out.println (Strpagebean.getmessagelist (). Get (0));106System.out.println (Strpagebean.getmessagelist (). Get (1));107 System.out.println (Strpagebean.getpageinfo (). Gettotal ());108     }109  the}

Results:------------

Solve complex JSON structures with Objectmapper readvalue () and generics

Related Article

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.