java - 使用 Jackson 进行 boolean 和数组数据绑定(bind)

标签 java json data-binding jackson databinder

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

如果结果是 boolean 值,则响应中收到的 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);
    }
}}

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

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.boolean out of START_OBJECT token

有办法解决这个问题吗?

最佳答案

您可以尝试将Builder.succeeded的类型更改为Object,然后添加一些代码以便稍后读取。这听起来像是 future 错误的来源,但如果您不控制 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,其余的数据的要么是顶级字段,要么是结果的成员:

{
  "succeeded": true,
  "version": 1.0,
  "current_page": 1,
  "per_page": 100,
  "results": [
    {
      "get_info": {
        "fieldA": "4198126",
        ...
      }
   ]
}

关于java - 使用 Jackson 进行 boolean 和数组数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646672/

相关文章:

java - MySQLIntegrityConstraintViolationException 使应用程序崩溃

javascript - 基本 JSON.parse 问题

c# - 远程验证显示红框但没有错误信息? ASP-MVC 5

c# - MVC 在复杂的 View 模型上添加具有绑定(bind)的表行

Java对象空检查方法

Java 8 : Lambda expression contains more than one statement/logic

java - 基于行起始符分割文本文件

javascript - 如何在不使用 slurp 标志的情况下将 JSON 序列与 jq 结合起来?

java - 将 JSON 子对象属性绑定(bind)到 Jackson 中的 Java 对象字段

wpf - 如何使用过滤器集自动刷新 ListCollectionView