简体   繁体   English

在Jackson中反序列化JSON属性

[英]Deserialize JSON property in Jackson

I have a JSON file as such: 我有这样的JSON文件:

{
    "id1" : {
       ...
    },
    "id2" : {
       ...
    }
}

I am using mapper.readerForUpdating(object).readValue(jsonFile) and want the reader to read only the data corresponding to the id that object 's class has provided in an annotation. 我正在使用mapper.readerForUpdating(object).readValue(jsonFile) ,希望读者仅读取与该object的类在批注中提供的ID相对应的数据。 Is there a way to annotate object 's class with "id1" or something so that mapper will only read values from "id1"? 有没有一种方法可以用“ id1”或其他注释object的类,以便mapper仅从“ id1”中读取值?

I can think of one simple solution if your id1 is static. 如果您的id1是静态的,我可以想到一个简单的解决方案。 Create a wrapper class which contains one getter (and setter) corresponding to the field you want and ignore all the other properties as follows: 创建一个包装器类,其中包含一个与所需字段相对应的getter(和setter), 忽略所有其他属性,如下所示:

@JsonProperty("id1")
public MyBean getCustomId()
{
     ...
}

@JsonProperty("id1")
public void setCustomId()
{
     ...
}

Second option would be to use @JsonSerialize and @JsonDeserialize annotations and write your customized logic. 第二种选择是使用@JsonSerialize@JsonDeserialize批注并编写您的自定义逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM