java - Jackson 将Value 转换为List 但返回null

标签 java json jackson

json字符串是这样的:

[
   {
     "id":1545043316459,
     "user_id":127118102570729472,
     "username":"abcd",
     "phone":"12413453563",
     "password":"b6b33aad5c337315d4ac5a34d82db990cfaf22e5eefe27220b6827d5e66ad8b4",
     "license":"12351331",
     "plate":"abcdef",
     "avatar":"",
     "approval":0,
     "balance":0.0
   }
]

当我使用ObjectMapper.convertValue()时将此 json 转换为 List<User> ,该方法返回 null。但是,当我将字段“approval”更改为1时,转换成功。
第T类User的构造函数如下:

@JsonCreator
public User(@JsonProperty("user_id") long userId, @JsonProperty("username") String username, @JsonProperty("password") String password, @JsonProperty("avatar") String avatar,
                @JsonProperty("phone") String phone, @JsonProperty("license") String license, @JsonProperty("plate") String plate, @JsonProperty("approval") int approval,
                @JsonProperty("balance") BigDecimal balance) {
   this.userId = userId;
   this.username = username;
   this.password = password;
   this.avatar = avatar;
   this.phone = phone;
   this.license = license;
   this.plate = plate;
   this.balance = balance;
   this.approval = approval;
}

我已申请@JsonIgnoreProperties(ignoreUnknown = true)到类(class)。

最佳答案

Jackson 忽略具有默认值的原始类型。

要解决此问题,您可以:

  1. 在字段上添加注释批准

    @JsonInclude(Include.NON_DEFAULT)
    
  2. 将类型从 int 更改为 Integer

解决方案取决于你

关于java - Jackson 将Value 转换为List 但返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53814040/

相关文章:

java - 为Java Jersey Restful Web服务添加身份验证机制

java - startActivityForResult 强制关闭我的应用程序

javascript - 为什么我的 Node 表达响应返回空?

java - jackson 解析javascript函数

java - 如何在 Jersey 资源方法中覆盖@JsonView

java - jackson 多态性: Nested Subtypes

java - 真正的大数据如何在数据科学家老死之前导入HDFS?

java - 使用 google-cloud-speech 时在 APK project.properties 中复制的重复文件

javascript - 简单的 JSON 问题...如何插入变量?

javascript - 使用 jsonParse 解析数据