zoukankan      html  css  js  c++  java
  • Jackson 将数组json转List泛型

    闲话不多说,直接上干活,见代码就懂。

    package com.zzcloud.job.common;
    
    import java.util.ArrayList;
    import java.util.List;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.zzcloud.job.common.entity.CheckItemsChild;
    
    public class Test {
    
    	public static void main(String[] args) throws Exception {
    		String json = "[{
    " + 
    				"	"checkItemsId": "1",
    " + 
    				"	"checkItemsName": "aaaaaa1",
    " + 
    				"	"checkPosition": "aaaaaa"
    " + 
    				"}, {
    " + 
    				"	"checkItemsId": "2",
    " + 
    				"	"checkItemsName": "bbbbbbb1",
    " + 
    				"	"checkPosition": "bbbbbbb"
    " + 
    				"}, {
    " + 
    				"	"checkItemsId": "3",
    " + 
    				"	"checkItemsName": "ccccccc1",
    " + 
    				"	"checkPosition": "ccccccc"
    " + 
    				"}, {
    " + 
    				"	"checkItemsId": "4",
    " + 
    				"	"checkItemsName": "dddddddddd1",
    " + 
    				"	"checkPosition": "dddddddddd"
    " + 
    				"}, {
    " + 
    				"	"checkItemsId": "5",
    " + 
    				"	"checkItemsName": "eeeeeeeeee1",
    " + 
    				"	"checkPosition": "eeeeeeeeee"
    " + 
    				"}, {
    " + 
    				"	"checkItemsId": "6",
    " + 
    				"	"checkItemsName": "ffffffff1",
    " + 
    				"	"checkPosition": "ffffffff"
    " + 
    				"}]
    " + 
    				"";
    		ObjectMapper objMapper = new ObjectMapper();
    		JavaType javaType = getCollectionType(ArrayList.class, CheckItemsChild.class); 
    		List<CheckItemsChild> list = (List<CheckItemsChild>)objMapper.readValue(json, javaType );
    		list.stream().forEach(p -> System.out.println(p.getCheckItemsName()));
    		
    	}
    	
    	public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { 
    		ObjectMapper mapper = new ObjectMapper();
    		return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); 
    	}
    
    }
    
  • 相关阅读:
    2019 SDN上机第5次作业
    SDN课程阅读作业(2)
    第05组 Alpha事后诸葛亮
    Ryu控制器编程开发——packet_in和packet_out简易交换机实现
    Ryu控制器安装部署和入门
    OpenDayLight Beryllium版本 下发流表实现hardtimeout
    Raspberry Pi 4B FTP服务器配置
    利用Wireshark抓取并分析OpenFlow协议报文
    基于OVS命令的VLAN实现
    利用Python脚本完成一个Fat-tree型的拓扑
  • 原文地址:https://www.cnblogs.com/jimmyshan-study/p/11706364.html
Copyright © 2011-2022 走看看