繁体   English   中英

Java:我的 JSON 没有正确反序列化为等效的 Java class?

[英]Java: My JSON not getting deserialized properly into its equivalent Java class?

我有以下 json,我想将其反序列化为 class。我没有用于组成 json 的 java 类。所以我尝试自己编写这些类。

但是,反序列化时会出现问题。
body is always null

那里的所有专家,你能帮我弄清楚我做错了什么吗?
我会非常感谢你的帮助

JSON Input:
{
    "totalSize": 2,
    "done": true,
    "records": [
      {
        "attributes": {
          "type": "record",
          "url": "a string"
        },
        "Body": "a string + something additional"
      },
      {
        "attributes": {
          "type": "record",
          "url": "b string"
        },
        "Body": "b string + something additional"
      }
    ]
  }
Code that does the deserialization:
final Response response = new ObjectMapper().readValue(json, Response.class);
Java Classes Input:

    @Getter
    @Setter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class Attributes {
        @NotBlank
        private String type;

        @NotBlank
        private String url;
    }

    @Getter
    @Setter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class Record {
        private Attributes attributes;

        @NotBlank
        public String body;
    }

    @Getter
    @Setter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class Response {
        private int totalSize;

        private boolean done;

        private List<Record> records;
    }

我没有看到属性名称是Body而不是body

因此,我需要使用以下注释来使其工作:

@JsonProperty("Body")

i.e., 


    @Getter
    @Setter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class Record {
        private Attributes attributes;

        @NotBlank
        @JsonProperty("Body")
        public String body;
    }

暂无
暂无

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

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