利用ObjectMapper readValue()和泛型解決複雜json結構

來源:互聯網
上載者:User

標籤:print   賦值   方法   stp   stat   abi   recv   ota   sage   

  1 import com.dj.fss.vo.MessageListVO;  2 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;  3 import com.fasterxml.jackson.core.JsonParseException;  4 import com.fasterxml.jackson.databind.JsonMappingException;  5 import com.fasterxml.jackson.databind.ObjectMapper;  6   7 import java.io.IOException;  8 import java.util.List;  9  10  11 /** 12  * ObjectMapper readValue()方法 13  * 利用泛型解決複雜json結構 14  * @JsonIgnoreProperties(ignoreUnknown = true) 沒有執行個體化的屬性不自動賦值 沒有這個回報異常 15  * 16  * @param <T> 17  */ 18 @JsonIgnoreProperties(ignoreUnknown = true) 19 public class PageBean<T extends Object> { 20     private List<MessageListVO> messageList; 21  22     private pageinfo pageinfo; 23  24     public PageBean.pageinfo getPageinfo() { 25         return pageinfo; 26     } 27  28     public void setPageinfo(PageBean.pageinfo pageinfo) { 29         this.pageinfo = pageinfo; 30     } 31  32     public List<MessageListVO> getMessageList() { 33         return messageList; 34     } 35  36     public void setMessageList(List<MessageListVO> messageList) { 37         this.messageList = messageList; 38     } 39  40     @JsonIgnoreProperties(ignoreUnknown = true) 41     public class pageinfo{ 42         private int total; 43  44         public int getTotal() { 45             return total; 46         } 47  48         public void setTotal(int total) { 49             this.total = total; 50         } 51     } 52  53  54  55  56     private static String jsonString = "{\n" + 57             "        \"messageList\": [\n" + 58             "            {\n" + 59             "                \"id\": \"7\",\n" + 60             "                \"sendCompany\": \"廣州機場\",\n" + 61             "                \"recvCompany\": \"中國南航航空公司\",\n" + 62             "                \"messageType\": \"1001\",\n" + 63             "                \"sendTime\": \"2018-07-12 16:43:06\"\n" + 64             "            },\n" + 65             "            {\n" + 66             "                \"id\": \"4\",\n" + 67             "                \"sendCompany\": \"廣州機場\",\n" + 68             "                \"recvCompany\": \"中國南航航空公司\",\n" + 69             "                \"messageType\": \"1002\",\n" + 70             "                \"sendTime\": \"2018-07-12 15:49:02\"\n" + 71             "            }\n" + 72             "        ],\n" + 73             "        \"pageinfo\": {\n" + 74             "            \"pageNum\": 1,\n" + 75             "            \"pageSize\": 3,\n" + 76             "            \"size\": 2,\n" + 77             "            \"startRow\": 1,\n" + 78             "            \"endRow\": 2,\n" + 79             "            \"total\": 2,\n" + 80             "            \"pages\": 1,\n" + 81             "            \"list\": null,\n" + 82             "            \"prePage\": 0,\n" + 83             "            \"nextPage\": 0,\n" + 84             "            \"isFirstPage\": true,\n" + 85             "            \"isLastPage\": true,\n" + 86             "            \"hasPreviousPage\": false,\n" + 87             "            \"hasNextPage\": false,\n" + 88             "            \"navigatePages\": 8,\n" + 89             "            \"navigatepageNums\": [\n" + 90             "                1\n" + 91             "            ],\n" + 92             "            \"navigateFirstPage\": 1,\n" + 93             "            \"navigateLastPage\": 1,\n" + 94             "            \"firstPage\": 1,\n" + 95             "            \"lastPage\": 1\n" + 96             "        }\n" + 97             "    }"; 98  99     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {100 101 102 103         ObjectMapper mapper = new ObjectMapper();104         PageBean<MessageListVO> strPageBean = mapper.readValue(jsonString, PageBean.class);105         System.out.println(strPageBean.getMessageList().get(0));106         System.out.println(strPageBean.getMessageList().get(1));107         System.out.println(strPageBean.getPageinfo().getTotal());108     }109 110 }

 

結果:------------

利用ObjectMapper readValue()和泛型解決複雜json結構

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.