Cannot instantiate a value of type java.util.linkedhashmap from a string value (‘{‘); there is no single string constructor / factory method

I have the following two 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:

There seems to be a problem with the map, but I think Jackson should be able to handle the map When I changed the map to map, I encountered the same problem But I do need to support various classes as map values

Solution

You have quotation marks around the payload object Try changing this section:

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

Enter this:

\"payload\":{\"to\":1}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>