【发布时间】:2016-10-16 21:01:19
【问题描述】:
我有一个来自外部源的 JSON 结构,并且在尝试构建可用于反序列化的类结构时遇到了一些困难。我已经注册了 json 数组的每个成员都有一个数组项的名称(oneDevice) - 我第一次看到这个
{
"pushResponse" : [
{
"oneDevice" : {
"status" : "Error 0",
"token" : "7676jbhjh",
"type" : "gcm"
}
},
{
"oneDevice" : {
"status" : "Error 0",
"token" : "asdasf66a",
"type" : "gcm"
}
}
]}
我尝试创建以下类结构,但在运行代码时收到错误消息:
@JsonIgnoreProperties( ignoreUnknown = true )
public class PushOkResponse {
@JsonProperty("pushResponse")
@XmlElement(required = true)
protected PushResponseType pushResponse;
}
@JsonIgnoreProperties( ignoreUnknown = false )
public class PushResponseType {
@JsonProperty( "oneDevice" )
protected List<OneDeviceType> oneDevice;
}
@JsonIgnoreProperties( ignoreUnknown = false )
public class OneDeviceType {
@XmlElement( required = true )
@JsonProperty( "status" )
protected String status;
@XmlElement( required = true )
@JsonProperty( "token" )
private String token;
@XmlElement( required = true )
@JsonProperty( "type" )
private String type;
}
PushOkResponse pushOkResponse = mapper.readValue(jsonInString, PushOkResponse.class);
com.fasterxml.jackson.databind.JsonMappingException 失败:无法反序列化 (packageName ommitted) 的实例...PushResponseType 超出 START_ARRAY 令牌 在[来源:{“pushResponse”:[{“oneDevice”:{“状态”:“错误0”,“令牌”:“7676jbhjh”,“类型”:“gcm”}}]}; line: 1, column: 22] (通过引用链: (packageName ommitted)...PushOkResponse["pushResponse"])
【问题讨论】: