Jackson将json字符串转换成泛型List,Map

需要先反序列化复杂类型 为泛型的Collection Type

如果是ArrayList那么使用ObjectMapper 的getTypeFactory().constructParametricType(ArrayList.class, YourBean.class);

ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, YourBean.class));
List lst =  (List)mapper.readValue(jsonString, javaType); 

如果是HashMap那么 ObjectMapper 的getTypeFactory().constructParametricType(HashMap.class,String.class, YourBean.class);

你可能感兴趣的:(java)