Java <;T>;无法解析为类型?
我想使用jackson json库将json字符串转换为Java <;T>;无法解析为类型?,java,json,generics,jackson,objectmapper,Java,Json,Generics,Jackson,Objectmapper,我想使用jackson json库将json字符串转换为列表 public static List<T> toList(String json, Class<T> type, ObjectMapperProperties objectMapperProperties){ ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(objectMapperProperties);
列表
public static List<T> toList(String json, Class<T> type, ObjectMapperProperties objectMapperProperties){
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(objectMapperProperties);
try {
return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, type));
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我猜泛型部分在这里我不清楚。非常感谢您的帮助。您需要在通用方法中首先声明
T
,在您的情况下,它将是:
public static <T> List<T> toList(String json, Class<T> type, ObjectMapperProperties objectMapperProperties)
公共静态列表toList(字符串json、类类型、ObjectMapperProperties ObjectMapperProperties)
有关更多信息,请查看oracle文档中的通用方法:
问题在于编译器上下文中的“T”可能是与其他类型类似的类型。使用字母tuv是我们用于泛型的约定,但您也可以轻松地编写列表。为了告诉编译器它是泛型,您必须做一些事情来区分它,否则它只是一个无法解析的类型。
public static <T> List<T> toList(String json, Class<T> type, ObjectMapperProperties objectMapperProperties)