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 忽略具有默认值的原始类型。
要解决此问题,您可以:
在字段上添加注释批准
@JsonInclude(Include.NON_DEFAULT)
将类型从
int
更改为Integer
解决方案取决于你
关于java - Jackson 将Value 转换为List 但返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53814040/