无法从字符串值 ('{') 实例化 java.util.LinkedHashMap 类型的值;没有单字符串构造函数/工厂方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32432104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:11:23  来源:igfitidea点击:

Cannot instantiate value of type java.util.LinkedHashMap from String value ('{'); no single-String constructor/factory method

javajsonHymansonfasterxml

提问by checklist

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.Hymanson.databind.JsonMappingException: Can not instantiate value of type java.util.LinkedHashMap from String value ('{'); no single-String constructor/factory method at [Source: {"changes":[{"epoch":1441556306522,"payload":"{"to":1}"},{"epoch":1441555481524,"payload":"{"to":-1}"}],"more":false}; line: 1, column: 35] (through reference chain: demo.ChangesJSON["changes"]->java.util.ArrayList[0]->demo.ChangeJSON["payload"])

com.fasterxml.Hymanson.databind.JsonMappingException:无法从字符串值('{')中实例化 java.util.LinkedHashMap 类型的值;[Source: {"changes":[{"epoch":1441556306522,"payload":"{"to":1}"},{"epoch":1441555481524,"payload" 没有单字符串构造函数/工厂方法:"{"to":-1}"}],"more":false}; 行:1,列:35](通过参考链:demo.ChangesJSON["changes"]->java.util.ArrayList[0]->demo.ChangeJSON["payload"])

It seems that there is an issue with the map but I thought Hymanson should be able to handle maps. I get the same issue also when I change the map to Map. But I do need to support all sorts of classes as the values of the map.

地图似乎有问题,但我认为Hyman逊应该能够处理地图。当我将地图更改为 Map 时,我也遇到了同样的问题。但我确实需要支持各种类作为地图的值。

回答by Petter

You have quotes around the payloadobject. Try changing this part:

您在payload对象周围有引号。尝试更改此部分:

\"payload\":\"{\"to\":1}\"

into this:

进入这个:

\"payload\":{\"to\":1}

回答by mprivat

I think it's the JSON itself that has a problem. It unescapes to:

我认为是 JSON 本身有问题。它转义为:

{"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}";