Jackson反序列化JSON选择字段

huangapple go评论68阅读模式
英文:

Jackson deserializing json choosen fields

问题

以下是您要翻译的内容:

有可能只对选择的字段进行 JSON 反序列化吗?
例如:

{
    "Version": 1,
    "Key": "353301_PC",
    "Type": "PostalCode",
    "Rank": 500,
    "LocalizedName": "Kleosin",
    "EnglishName": "Kleosin",
    "PrimaryPostalCode": "16-001",
    "Region": {
      "ID": "EUR",
      "LocalizedName": "Europe",
      "EnglishName": "Europe"
    }

我只想要 LocalizedName 和 EnglishName。尝试过使用 objectMapper 但是出现了错误。

英文:

there is possibility to deserialize Json only choosen fields?
Eg:

{
    "Version": 1,
    "Key": "353301_PC",
    "Type": "PostalCode",
    "Rank": 500,
    "LocalizedName": "Kleosin",
    "EnglishName": "Kleosin",
    "PrimaryPostalCode": "16-001",
    "Region": {
      "ID": "EUR",
      "LocalizedName": "Europe",
      "EnglishName": "Europe"
    }

And i want only LocalizedName and EnglishName. Tried with objectMapper but getting errors.

答案1

得分: 1

在你的数据类中添加 JsonIgnoreProperties 注解:

@JsonIgnoreProperties(ignoreUnknown = true)
public class YourClass {

   private String LocalizedName;

   private String EnglishName;
   
   ...
}
英文:

Add JsonIgnoreProperties annotation to your data class

@JsonIgnoreProperties(ignoreUnknown = true)
public class YourClass {

   private String LocalizedName;

   private String EnglishName;
   
   ...
}

答案2

得分: 0

你可以在 Region 类的字段上添加 @JsonIgnore。

英文:

you can add @JsonIgnore to the fields inside the Region class

huangapple
  • 本文由 发表于 2020年9月30日 19:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64136830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定