[英]Cannot instantiate value of type java.util.LinkedHashMap from String value ('{'); no single-String constructor/factory method
I have the following 2 classes: 我有以下两个班级:
@JsonIgnoreProperties(ignoreUnknown = true)
public class ChangesJSON {
@JsonProperty("changes")
List<ChangeJSON> changes;
@JsonProperty("more")
Boolean more;
}
public class ChangeJSON {
@JsonProperty("epoch")
Long epoch;
@JsonProperty("payload")
Map<String, Object> payload;
}
When I try to deserialize using this test: 当我尝试使用此测试反序列化时:
String test = "{\"changes\":[{\"epoch\":1441556306522,\"payload\":\"{\"to\":1}\"},{\"epoch\":1441555481524,\"payload\":\"{\"to\":-1}\"}],\"more\":false}";
@Test
public void myTest() {
ObjectMapper mapper = new ObjectMapper();
ChangesJSON result = null;
try {
result = mapper.readValue(test, ChangesJSON.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertNotNull(result);
}
I get the following exception: 我得到以下异常:
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type java.util.LinkedHashMap from String value ('{'); com.fasterxml.jackson.databind.JsonMappingException:无法从String值('{')实例化java.util.LinkedHashMap类型的值; no single-String constructor/factory method at [Source: {"changes":[{"epoch":1441556306522,"payload":"{"to":1}"},{"epoch":1441555481524,"payload":"{"to":-1}"}],"more":false}; [Source:{“changes”:[{“epoch”:1441556306522,“payload”:“{”to“:1}”},{“epoch”:1441555481524,“payload”中没有单字符串构造函数/工厂方法: “{” 到 “: - 1}”}], “更多”:假}; line: 1, column: 35] (through reference chain: demo.ChangesJSON["changes"]->java.util.ArrayList[0]->demo.ChangeJSON["payload"]) line:1,column:35](通过引用链:demo.ChangesJSON [“changes”] - > java.util.ArrayList [0] - > demo.ChangeJSON [“payload”])
It seems that there is an issue with the map but I thought Jackson should be able to handle maps. 似乎地图存在问题,但我认为杰克逊应该能够处理地图。 I get the same issue also when I change the map to Map. 当我将地图更改为Map时,我也遇到了同样的问题。 But I do need to support all sorts of classes as the values of the map. 但我确实需要支持各种类作为地图的值。
You have quotes around the payload
object. 您在payload
对象周围有引号。 Try changing this part: 尝试更改此部分:
\"payload\":\"{\"to\":1}\"
into this: 进入这个:
\"payload\":{\"to\":1}
I think it's the JSON itself that has a problem. 我认为这是JSON本身存在问题。 It unescapes to: 它不受欢迎:
{"changes":[{"epoch":1441556306522,"payload":"{"to":1}"},{"epoch":1441555481524,"payload":"{"to":-1}"}],"more":false}
It should probably be something like: 它可能应该是这样的:
{"changes":[{"epoch":1441556306522,"payload":{"to":1}},{"epoch":1441555481524,"payload":{"to":-1}}],"more":false}
So: 所以:
String test = "{\"changes\":[{\"epoch\":1441556306522,\"payload\":{\"to\":1}},{\"epoch\":1441555481524,\"payload\":{\"to\":-1}}],\"more\":false}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.