0

我有以下格式的 JSON:

{
    "FileStatuses": {
        "FileStatus": [{
                "accessTime": 1479784299020,
                "type": "FILE"
            },
            {
                "accessTime": 1475421868510,
                "type": "FILE"
            }
        ]
    }
}

我正在尝试使用以下类对其进行反序列化:

@Value.Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableFileListResponse.class)
@JsonDeserialize(as = ImmutableFileListResponse.class)
public interface FileListResponse {

    @JsonProperty("FileStatuses")
    JSONObject fileStatuses();
}

但它会引发以下错误:unrecognized field "FileStatus", not marked as ignorable (0 known properties )

但是,如果我将 Json 读取为字符串然后使用JSONObject obj = new JSONObject(source);它就可以完美地工作。

我哪里错了?

4

1 回答 1

0

我使用以下两个类让它工作:

@Value.Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableFileListResponse.class)
@JsonDeserialize(as = ImmutableFileListResponse.class)
public interface FileListResponse {

    @JsonProperty("FileStatuses")
    FileListInnerResponse fileStatuses();

    @Value.Immutable
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonSerialize(as = ImmutableFileListInnerResponse.class)
    @JsonDeserialize(as = ImmutableFileListInnerResponse.class)
    interface FileListInnerResponse {

        @JsonProperty("FileStatus")
        List<FileData> fileStatus();
    }
}

和 FileData.java:

@Value.Immutable
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(as = ImmutableFileData.class)
@JsonDeserialize(as = ImmutableFileData.class)
public interface FileData {

    @JsonProperty("accessTime")
    long accessTime();

    @JsonProperty("type")
    String type();
}

请让我知道是否有比使用内部类或 3 个不同的类更好的方法。

于 2020-06-29T21:45:03.420 回答