简体   繁体   English

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

[英]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.

相关问题 无法从JSON String实例化类型的值; 没有单字符串构造函数/工厂方法 - Can not instantiate value of type from JSON String; no single-String constructor/factory method 无法从JSON字符串实例化[简单类型,类org.joda.time.LocalDateTime]类型的值;没有单字符串构造函数/工厂方法 - Cannot instantiate value of type [simple type, class org.joda.time.LocalDateTime] from JSON String;no single-String constructor/factory method 无法实例化类型 [...] 的值没有单字符串构造函数 - Can not instantiate value of type [...] no single-String constructor JsonMappingException:没有单字符串构造函数/工厂方法 - JsonMappingException: no single-String constructor/factory method 无法构造java.util.LinkedHashMap的实例:no String-argument constructor / factory - Can not construct instance of java.util.LinkedHashMap: no String-argument constructor/factory class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String - class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String 如何使用java.util.LinkedHashMap $ EntryIterator类型访问对象值 - How to access the object value with java.util.LinkedHashMap$EntryIterator type 无法将类型“java.util.LinkedHashMap”类型的现有声明值转换为所需类型 class - Cannot convert existing claim value of type 'class java.util.LinkedHashMap' to desired type class Jackson JSON - 解组时“没有单字符串构造函数/工厂方法”错误 - Jackson JSON - “no single-String constructor/factory method” error on unmarshal 收到以下错误,没有单字符串构造函数/工厂方法 - Getting the following error no single-String constructor/factory method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM