我正在编写以下代码将我的 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>>(){})
可能出现什么错误?
问题在于导入 TypeReference。我正在使用
import com.fasterxml.jackson.core.type.TypeReference
而不是
import org.codehaus.jackson.type.TypeReference
对我来说,问题在于导入 ObjectMapper。我用的是
导入com.fasterxml.jackson.core.type.ObjectMapper
而不是
导入org.codehaus.jackson.type.ObjectMapper
对于我遇到的同样问题,解决方案是为 MyObject 类实现 setter 和 getter。