【问题标题】:Parsing nested objects with Jackson用 Jackson 解析嵌套对象
【发布时间】:2015-06-19 13:49:07
【问题描述】:

我正在使用 Robospice + Retrofit + Jackson。我没有普通类,它有另一个类对象作为字段。我需要解析 json 并使用字段创建类。
这是我的课

@JsonIgnoreProperties(ignoreUnknown=true)
public class User implements UserInformationProvider {
    @JsonProperty("customer_id")
    public int id;
    @JsonProperty("firstname")
    public String firstName;
    @JsonProperty("lastname")
    public String lastName;
    @JsonProperty("email")
    public String email;
    @JsonProperty("telephone")
    public String phone;
    @JsonProperty("token_api")
    public String token;
    @JsonProperty("token_expire")
    public int tokenExpireTime;
    public UserPreferences userPreferences;
    @Override
    public String getUserFirstName() {
        return firstName;
    }

    @Override
    public String getUserLastName() {
        return lastName;
    }

    @Override
    public String getUserEmail() {
        return email;
    }

    @Override
    public String getUserIconUrl() {
        return null;
    }
}

和偏好类

public class UserPreferences {
    public boolean offersNotifications;
    public boolean statusChangedNotifications;
    public boolean subscriptionNotifications;
    @JsonProperty("new_offers")
    public boolean newOffersNotify;
    @JsonProperty("order_status_changed")
    public boolean orderStatusChangedNotify;
    @JsonProperty("hot_offers")
    public boolean hotOffersNotify;
}

我需要解析成 POJO 的请求。

{
    "customer_id": 84,
    "token_api": "ef5d7d2cd5dfa27a",
    "token_expire_unix": "1435113663",
    "preferences": {
        "new_offers": "1",
        "order_status_changed": "1",
        "hot_offers": "1"
    }
}

请帮忙,我如何使用 Jackson 来做到这一点。如果有任何帮助,我将不胜感激。提前致谢。

【问题讨论】:

    标签: java json parsing jackson


    【解决方案1】:

    主要问题在于UserPreferences 内部。现在您的代码正试图将"1" 反序列化为boolean。 Java 不会为您执行此转换,因此您需要创建一个自定义反序列化器并将其应用于具有数字布尔值的字段。

    创建自定义反序列化程序

    反序列化器允许您指定一个类并将自定义操作应用于从 JSON 创建它的方式:

    public class NumericBooleanDeserializer extends JsonDeserializer<Boolean> {
        @Override
        public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            int intValue = p.getValueAsInt();
            switch (intValue) {
                case 0:
                    return Boolean.TRUE;
                case 1:
                    return Boolean.FALSE;
                default:
                    // throw exception or fail silently
            }
            return null; // can throw an exception if failure is desired
        }
    }
    

    对字段应用自定义反序列化

    由于您可能不想在 ObjectMapper 上注册它并将其应用于 all 反序列化,因此您可以使用 @JsonDeserialize 注释。您的 UserPreferences 课程最终会看起来像这样:

    public class UserPreferences {
        public boolean offersNotifications;
        public boolean statusChangedNotifications;
        public boolean subscriptionNotifications;
    
        @JsonProperty("new_offers")
        @JsonDeserialize(using = NumericBooleanDeserializer.class)
        public boolean newOffersNotify;
    
        @JsonProperty("order_status_changed")
        @JsonDeserialize(using = NumericBooleanDeserializer.class)
        public boolean orderStatusChangedNotify;
    
        @JsonProperty("hot_offers")
        @JsonDeserialize(using = NumericBooleanDeserializer.class)
        public boolean hotOffersNotify;
    }
    

    确保 @JsonProperty 匹配 JSON 密钥

    由于您的 JSON 具有 "preferences" 并且您的 Java 属性的名称是 userPreferences,因此您需要在 User 内部的属性上添加一个 @JsonProperty("preferences")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      相关资源
      最近更新 更多