这是我用于反序列化从 Google App Engine 云端点返回的响应的代码:
String jsonString = IOUtils.toString(
httpResponse.getEntity().getContent(), "UTF-8");
ObjectMapper mapper = new ObjectMapper();
ArrayList<myPOJO> myList=
mapper.readValue(jsonString, new TypeReference<ArrayList<MyPOJO>>(){});
jsonString 看起来像这样:
{
"items" : [ {
"id" : "12345",
"name" : "test1"
}, {
"id" : "121212",
"name" : "test2"
} ]
}
但是我收到了这个错误:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@547a7880; line: 1, column: 1]
使用 Jackson 将此 JSON 反序列化为 POJO 列表的正确方法是什么?
最佳答案
尝试直接用映射器反序列化
String jsonString = IOUtils.toString(
httpResponse.getEntity().getContent(), "UTF-8");
ObjectMapper mapper = new ObjectMapper();
List<MyPOJO> myList= mapper.convertValue(jsonString, mapper.getTypeFactory().constructCollectionType(List.class, MyPOJO.class));
关于java - 将 JSON 字符串反序列化为 POJO 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23571413/