jackson json 嵌套对象_java – 用Jackson解析嵌套对象

本文介绍了解析特定JSON格式的方法,特别是在处理布尔值时遇到的问题,并提供了一个具体的解决方案,即创建自定义反序列化器来解决布尔值解析问题。
部署运行你感兴趣的模型镜像

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

@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”)

您可能感兴趣的与本文相关的镜像

LobeChat

LobeChat

AI应用

LobeChat 是一个开源、高性能的聊天机器人框架。支持语音合成、多模态和可扩展插件系统。支持一键式免费部署私人ChatGPT/LLM 网络应用程序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值