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

【转】Jackson 处理复杂类型(List,map)两种方法

程序员文章站 2022-07-12 22:17:09
...

 

方法一:

 

String jsonString="[{'id':'1'},{'id':'2'}]";  
ObjectMapper mapper = new ObjectMapper();  
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);  
//如果是Map类型  mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);  
List<Bean> lst =  (List<Bean>)mapper.readValue(jsonString, javaType);   

 

方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";  
ObjectMapper mapper = new ObjectMapper();  
List<Bean> beanList = mapper.readValue(jsonString, new TypeReference<List<Bean>>() {});  

 

 

相关标签: Jackson json