欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用ObjectMapper readValue()和泛型解决复杂json结构

程序员文章站 2022-05-06 10:41:59
结果: ......
  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结构