我正在尝试反序列化从API调用返回的结果。但是结果可以包含布尔值或数组。
如果result是boolean,则响应中收到的json看起来像
{
"succeeded": true,
"version": 1.0
}
如果result是数组,json在响应中收到json看起来像
{
"succeeded": {
"current_page": 1,
"per_page": 100,
"results": [
{
"get_info": {
"fieldA": "4198126",
"fieldB": "2016-05-25T22:43:52Z",
"fieldC": "iws-user-cfg-proxy-beta",
"updated_at": "2016-05-25T22:43:52Z"
}
},
{
"get_info": {
"fieldA": "4551542",
"fieldB": "2016-07-27T22:26:27Z",
"fieldC": "silkRoot",
"updated_at": "2016-07-27T22:26:27Z"
}
}
]
},
"version": 1.0
}
我想读取与成功字段相关的值。有没有办法在映射类中处理这个问题。 我目前的映射类如下:
public class ServResp {
public final static String TYPE1_EXCEPTION = "Type1Exception";
public final static String TYPE2_EXCEPTION = "Type2Exception";
public final int httpStatusCode;
public final boolan succeeded;
public final String version;
public final String exception;
public final String exceptionMessage;
private ServResp(Builder builder) {
this.httpStatusCode = builder.httpStatusCode;
this.succeeded = builder.succeeded;
this.version = builder.version;
this.exception = builder.exception;
this.exceptionMessage = builder.exceptionMessage;
}
public Builder modify() {
return new Builder(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((exception == null) ? 0 : exception.hashCode());
result = prime * result + ((exceptionMessage == null) ? 0 : exceptionMessage.hashCode());
result = prime * result + httpStatusCode;
result = prime * result + (succeeded ? 17 : 19);
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ServResp other = (ServResp) obj;
if (exception == null) {
if (other.exception != null)
return false;
} else if (!exception.equals(other.exception))
return false;
if (exceptionMessage == null) {
if (other.exceptionMessage != null)
return false;
} else if (!exceptionMessage.equals(other.exceptionMessage))
return false;
if (httpStatusCode != other.httpStatusCode)
return false;
if (succeeded != other.succeeded)
return false;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
return true;
}
public static class Builder {
private int httpStatusCode;
private boolean succeeded;
private String version;
private String exception;
private String exceptionMessage;
public Builder() {
}
public Builder(ServResp other) {
this.httpStatusCode = other.httpStatusCode;
this.version = other.version;
this.exception = other.exception;
this.exceptionMessage = other.exceptionMessage;
}
public Builder setHttpStatusCode(int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
return this;
}
public Builder setSucceeded(boolean succeeded) {
this.succeeded = succeeded;
return this;
}
public Builder setVersion(String version) {
this.version = version;
return this;
}
public Builder setException(String exception) {
this.exception = exception;
return this;
}
public Builder setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
return this;
}
public ServResp build() {
return new ServResp(this);
}
}}
如果我按原样执行程序,我会收到以下错误:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.boolean out of START_OBJECT token
有没有办法解决这个问题?
由于
答案 0 :(得分:1)
我建议使用此工具jsonschematopojo为json生成普通的pojo。在生成pojo时,您可以选择Source类型为JSON和Annotation样式:none。
{
"succeeded": {
"current_page": 1,
"per_page": 100,
"results": [
{
"get_info": {
"fieldA": "4198126",
"fieldB": "2016-05-25T22:43:52Z",
"fieldC": "iws-user-cfg-proxy-beta",
"updated_at": "2016-05-25T22:43:52Z"
}
},
{
"get_info": {
"fieldA": "4551542",
"fieldB": "2016-07-27T22:26:27Z",
"fieldC": "silkRoot",
"updated_at": "2016-07-27T22:26:27Z"
}
}
]
}
}
将生成的bean添加到项目中后,可以在mapper类中添加此重载方法
private Succeeded succeeded;
/**
*
* @return
* The succeeded
*/
public Succeeded getSucceeded() {
return succeeded;
}
/**
*
* @param succeeded
* The succeeded
*/
public void setSucceeded(Succeeded succeeded) {
this.succeeded = succeeded;
}
请检查此方法是否有效。