java – 用Jackson解析嵌套对象

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

这是我的课

@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"
    }
}

请帮忙,我怎么能用杰克逊做到这一点.我会非常感谢任何帮助.提前致谢.

最佳答案 主要问题在于UserPreferences.现在你的代码试图将“1”反序列化为布尔值. 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上注册它并将其应用于所有反序列化,因此可以使用@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具有“首选项”并且您的Java属性的名称是userPreferences,因此您需要在User内部的属性上打一个@JsonProperty(“preferences”)

点赞