2

我正在尝试反序列化从 API 调用返回的结果。但是,结果可以包含布尔值或数组。

如果结果为布尔值,则响应中收到的 JSON 内容如下所示:

{
  "succeeded": true,
  "version": 1.0
}

如果结果是一个数组,则响应中收到的 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);
    }
}}

如果我按原样执行程序,我会收到以下错误:

原因:org.codehaus.jackson.map.JsonMappingException:无法从 START_OBJECT 令牌中反序列化 java.lang.boolean 的实例

有没有办法解决这个问题?

4

2 回答 2

1

我建议使用工具JSONschema2POJO为下面的 JSON 内容生成一个普通的POJO

在生成 POJO 时,您可以选择Source typeJSONAnnotation stylenone

{
    "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 添加到项目中后,您可以在映射器类中添加此重载方法:

private Succeeded succeeded;

/**
 *
 * @return
 *     The succeeded
 */
public Succeeded getSucceeded() {
    return succeeded;
}

/**
 *
 * @param succeeded
 *     The succeeded
 */
public void setSucceeded(Succeeded succeeded) {
    this.succeeded = succeeded;
}
于 2016-07-29T02:28:25.520 回答
1

您可以尝试将类型更改Builder.succeededObject,然后添加一些代码以供稍后阅读。这听起来像是未来错误的来源,但如果你不控制 API,那么它可能是你最好的选择。

public class Foo {
    private Object overRiddenJsonType;

    public Object getOverRiddenJsonType() {
        return overRiddenJsonType;
    }

    public void setOverRiddenJsonType(Object overRiddenJsonType) {
        this.overRiddenJsonType = overRiddenJsonType;
    }
}

public class FooConsumer {
    public void consumeFoo(Foo foo) {
        Boolean b = false;
        Bar bar = null;
        if (foo.getOverRiddenJsonType() instanceof Boolean) {
            b = (Boolean)foo.getOverRiddenJsonType();
            // Worry about an NPE from unboxing later...
        } else if (foo.getOverRiddenJsonType() instanceof Bar) {
            bar = (Bar)foo.getOverRiddenJsonType();
        }
        // ...
    }
}

另一方面,如果您确实控制了 API,那么更好的解决方案是重构您的 JSON,使其success始终为boolean,其余数据要么是顶级字段,要么是 的成员results

{
  "succeeded": true,
  "version": 1.0,
  "current_page": 1,
  "per_page": 100,
  "results": [
    {
      "get_info": {
        "fieldA": "4198126",
        ...
      }
   ]
}
于 2016-07-28T21:36:49.183 回答