readValue() 无法与 TypeReference 一起使用

问题描述 投票:0回答:3

我正在编写以下代码将我的 JSON 字符串转换为我的对象列表。

List<MyObject> myResponse = new ArrayList<MyObject>();
myResponse = new ObjectMapper().readValue(responseString, new TypeReference<List<MyObject>>(){});

但是 Eclipse 抱怨以下错误:

The method readValue(String, Class<T>) in the type ObjectMapper is not applicable for the arguments (String, new TypeReference<List<MyObject>>(){})

可能出现什么错误?

json jackson
3个回答
11
投票

问题在于导入 TypeReference。我正在使用

import com.fasterxml.jackson.core.type.TypeReference

而不是

import org.codehaus.jackson.type.TypeReference


1
投票

对我来说,问题在于导入 ObjectMapper。我用的是

导入com.fasterxml.jackson.core.type.ObjectMapper

而不是

导入org.codehaus.jackson.type.ObjectMapper


0
投票

对于我遇到的同样问题,解决方案是为 MyObject 类实现 setter 和 getter。

© www.soinside.com 2019 - 2024. All rights reserved.