JsonMappingException +无法反序列化

时间:2014-09-23 06:19:40

标签: json deserialization json-deserialization

这是我向社区发布的第一篇文章。我试图将有效负载反序列化为一个类,并一直在尝试失败。我经历了与网站异常相关的其他类似帖子,但没有解决我的问题。我要做的就是将有效负载映射到MessageBody.class类,并通过getMessage getter获取与元素'Message'对应的有效负载。你能帮忙解决这个问题吗?

由于

MessageBody类

@JsonIgnoreProperties(ignoreUnknown = true)
public class MessageBody {

    @SerializedName("Message")
    @JsonProperty("Message")
    private String mMessage;

    public MessageBody() {
    }

    public MessageBody(String message) {
        this.mMessage = message;
    }

    public String getMessage() {
        return mMessage;
    }

    public void setMessage(String message) {
        this.mMessage = message;
    }
}

反序列化尝试代码

private static ObjectMapper objectMapper = new ObjectMapper();

public static void main(String[] args) throws IOException {

    String payload = "{\"Message\" : {\"simpleId\":{\"id\":\"44\"},\"dV\":{\"v\":1},\"pId\":{\"pId\":\"977\"},\"state\":\"start\"}}";
    MessageBody messageBody = objectMapper.readValue(
                payload.getBytes(StandardCharsets.UTF_8), MessageBody.class);
}
 

抛出异常

 [java] Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
     [java]  at [Source: [B@45812b51; line: 1, column: 2] (through reference chain: MessageBody["Message"])
     [java] 	at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
     [java] 	at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
     [java] 	at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:44)
     [java] 	at org.codehaus.jackson.map.deser.std.StringDeserializer.deserialize(StringDeserializer.java:13)
     [java] 	at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
     [java] 	at org.codehaus.jackson.map.deser.SettableBeanProperty$FieldProperty.deserializeAndSet(SettableBeanProperty.java:579)
     [java] 	at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
     [java] 	at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
     [java] 	at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
     [java] 	at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1935)

0 个答案:

没有答案