首页 > 编程语言 > 详细

Jackson 将数组json转List泛型

时间:2019-10-20 00:18:15      阅读:75      评论:0      收藏:0      [点我收藏+]

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

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 = "[{\r\n" + 
				"	\"checkItemsId\": \"1\",\r\n" + 
				"	\"checkItemsName\": \"aaaaaa1\",\r\n" + 
				"	\"checkPosition\": \"aaaaaa\"\r\n" + 
				"}, {\r\n" + 
				"	\"checkItemsId\": \"2\",\r\n" + 
				"	\"checkItemsName\": \"bbbbbbb1\",\r\n" + 
				"	\"checkPosition\": \"bbbbbbb\"\r\n" + 
				"}, {\r\n" + 
				"	\"checkItemsId\": \"3\",\r\n" + 
				"	\"checkItemsName\": \"ccccccc1\",\r\n" + 
				"	\"checkPosition\": \"ccccccc\"\r\n" + 
				"}, {\r\n" + 
				"	\"checkItemsId\": \"4\",\r\n" + 
				"	\"checkItemsName\": \"dddddddddd1\",\r\n" + 
				"	\"checkPosition\": \"dddddddddd\"\r\n" + 
				"}, {\r\n" + 
				"	\"checkItemsId\": \"5\",\r\n" + 
				"	\"checkItemsName\": \"eeeeeeeeee1\",\r\n" + 
				"	\"checkPosition\": \"eeeeeeeeee\"\r\n" + 
				"}, {\r\n" + 
				"	\"checkItemsId\": \"6\",\r\n" + 
				"	\"checkItemsName\": \"ffffffff1\",\r\n" + 
				"	\"checkPosition\": \"ffffffff\"\r\n" + 
				"}]\r\n" + 
				"";
		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); 
	}

}

 

Jackson 将数组json转List泛型

原文:https://www.cnblogs.com/jimmyshan-study/p/11706364.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!