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