[英]Java/Jackson: Deserialize JSON into class with HashMap
I want to deserialize the following JSON input: 我想反序列化以下JSON输入:
{
"key1": "value1",
"key2": "value2"
}
into a class object containing a hash map: 到包含哈希映射的类对象中:
public class ClassContainingMap {
private Map<String, String> map = new HashMap<>();
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
When executing 执行时
ClassContainingMap m = objectMapper.readValue(json, ClassContaininMap.class);
I get 我懂了
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of test.ClassContainingMap: no Stri
ng-argument constructor/factory method to deserialize from String value ('key1')
at [Source: "key1": "value1", "key2": "value2"; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012)
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:370)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1283)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:159)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:150)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)
I found that I can get the other direction (serialization) working when annotating the getter with @JsonValue
, but so far I haven't been able to figure this out for deserialization. 我发现当使用
@JsonValue
注释getter时,我可以得到另一个方向(序列化)的工作,但是到目前为止,我还无法弄清楚反序列化的方向。 Is there any simple (annotation?) way to achieve this, without having to write a custom deserializer? 是否有任何简单的方法(注释?)来实现此目的,而无需编写自定义反序列化器?
You did not provide an example how you use the @JsonValue
annotation so I might have missed something, but anyway. 您没有提供如何使用
@JsonValue
批注的示例,因此无论如何我可能已经错过了一些东西。
When serializing I think you do something like: 序列化时,我认为您可以执行以下操作:
ClassContainingMap ccm = new ClassContainingMap();
ccm.getMap().put("key1", "value1");
ccm.getMap().put("key2", "value2");
System.out.println(om.writeValueAsString(ccm));
This will work and will produce the following JSON: 这将起作用,并将产生以下JSON:
{
"map": {
"key1":"value1",
"key2":"value2",
}
}
But this does not equal to the JSON structure you want to read to ClassContainingMap
: 但这不等于您要读取到
ClassContainingMap
的JSON结构:
{
"key1": "value1",
"key2": "value2"
}
Namely the former is data from ClassContainingMap
and the latter is "only" Map
that I think you want to populate inside ClassContainingMap
. 也就是说,前者是来自
ClassContainingMap
数据,而后者是“唯一” Map
,我想您要在ClassContainingMap
填充。
You have two options to check, you can; 您有两个选项可以检查;
ClassContainingMap
ClassContainingMap
相对应的源 just deserialize this to a simple map, like: 只需将其反序列化为简单的地图即可,例如:
Map map = om.readValue(i, Map.class);
and if either of above are possible, one way is to do it with @JsonCreator
并且如果以上任何一种可行,一种方法是使用
@JsonCreator
@JsonCreator(mode=Mode.DELEGATING) public ClassContainingMap(@JsonProperty("map")Map<String,String> map) { this.map = map; } @JsonValue public Map<String, String> getMap() { return map; }
See some more information about 3rd for example here 例如, 在此处查看有关3rd的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.