gpt4 book ai didi

java - 无法解析为类型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:36:45 24 4
gpt4 key购买 nike

我想将 json 字符串转换为 List<Someclass>使用 jackson json 库。

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();
}
}

所以如果我通过 Attribute.class作为type , 那么它必须返回一个 List<Attribute> .

但是,这给了我一个编译时错误

T cannot be resolved to a type

我想我在这里不清楚泛型部分。感谢您的帮助。

最佳答案

你需要先在你的泛型方法中声明 T,在你的情况下它将是:

public static <T> List<T> toList(String json, Class<T> type,  ObjectMapperProperties objectMapperProperties)

有关更多信息,请查看 oracle 文档中的通用方法:

https://docs.oracle.com/javase/tutorial/extra/generics/methods.html

关于java - <T> 无法解析为类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35622101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com