用杰克逊解析嵌套对象

时间:2015-06-19 13:49:08

标签: java json parsing jackson

我正在使用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"
    }
}

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

1 个答案:

答案 0 :(得分:2)

主要问题在于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上注册此内容并将其应用于所有反序列化,因此您可以使用@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,因此您需要在@JsonProperty("preferences")

内的属性上拍一个User
相关问题