我正在尝试使用Jackson对对象进行反序列化,该对象是类型化对象的列表。当我尝试这样做时,我得到以下错误
无法从JSON String实例化
[map type; class java.util.HashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]]
类型的值;没有单字符串构造函数/工厂方法
到目前为止,我有以下内容,但似乎无效。
Terms.class
@JsonDeserialize(as=JsonMapDeserializer)
private List<ObjectA> results = null; //ommitted getter and setters
我的反序列化器类如下。
public class JsonMapDeserializer extends JsonDeserializer<List<ObjectA>> {
List<ObjectA> retMap = new ArrayList<ObjectA>();
TypeReference<HashMap<String,String>[]> typeRef = new TypeReference<HashMap<String,String>[]>() {};
@Override
public List<ObjectA> deserialize(JsonParser parser, DeserializationContext ctx)
throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
//read the json string into the map
HashMap<String, String>[] maps = mapper.readValue(parser, typeRef);
if(maps != null) {
for(HashMap<String, String> map : maps) {
ObjectA result = new ObjectA("id", map.get("id"));
retMap.add(result);
}
}
return retMap;
}
}
我正在使用简单模块添加反序列化器,如下所示
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("safety", Version.unknownVersion());
module.addDeserializer(List.class, new JsonMapDeserializer());
mapper.registerModule(module);
我最终尝试去串化的JSON字符串如下
"SearchTerms":{"results":[{id":"1010","checked":"true"}] // there are other fields I have just omitted them
当我运行代码反序列化时,我得到以下堆栈跟踪
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [map type; class java.util.HashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from JSON String; no single-String constructor/factory method (through reference chain: com.model.search["searchTerm1"])
at org.codehaus.jackson.map.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:379)
at org.codehaus.jackson.map.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:268)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:244)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:33)
at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:104)
at org.codehaus.jackson.map.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1294)
at com.az.rd.ke.json.JsonColumnMapDeserializer.deserialize(JsonColumnMapDeserializer.java:41)
at com.az.rd.ke.json.JsonColumnMapDeserializer.deserialize(JsonColumnMapDeserializer.java:27)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.
从查看堆栈跟踪和调试,因为我的模块已设置如下
module.addDeserializer(List.class, new JsonMapDeserializer());
反序列化时,一旦它到达我的对象中的第一个属性就是抱怨,这是一个列表,因为它所抱怨的searchTerm1
只是一个字符串列表。
有人可以告诉我如何反序列化类型对象列表,或者如何正确添加反序列化器。如果我将adddeserializer
方法更改为
module.addDeserializer(List<ObjectA>.class, new JsonMapDeserializer());
它有编译器问题,因为反序列化器类的类型为List<ObjectA>
。
答案 0 :(得分:0)
它看起来好像要反序列化的对象是.has-error {
border-color: red
}
,而不是Map<String, List<Map<String,String>>>
。
也许可以试试看?