Java Jackson将值转换为列表,但返回null
json字符串如下所示:Java Jackson将值转换为列表,但返回null,java,json,jackson,Java,Json,Jackson,json字符串如下所示: [ { "id":1545043316459, "user_id":127118102570729472, "username":"abcd", "phone":"12413453563", "password":"b6b33aad5c337315d4ac5a34d82db990cfaf22e5eefe27220b6827d5e66ad8b4", "license":"12351331", "p
[
{
"id":1545043316459,
"user_id":127118102570729472,
"username":"abcd",
"phone":"12413453563",
"password":"b6b33aad5c337315d4ac5a34d82db990cfaf22e5eefe27220b6827d5e66ad8b4",
"license":"12351331",
"plate":"abcdef",
"avatar":"",
"approval":0,
"balance":0.0
}
]
当我使用ObjectMapper.convertValue()
将此json转换为列表时,该方法返回null。但是,当我将字段“approval”更改为1时,转换成功
Th class用户的构造函数如下所示:
@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)
应用于该类 Jackson忽略具有默认值的基元类型
要解决此问题,您可以:
将注释放在字段批准上
@JsonInclude(Include.NON_DEFAULT)
将类型从int
更改为Integer
解决方案取决于您可能的副本