gpt4 book ai didi

java - Jackson - 反序列化嵌套的 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:34 24 4
gpt4 key购买 nike

我有一个 JSON 字符串,格式如下:

{
"response": {
"execution_status": "ready",
"report": {
"cache_hit": true,
"created_on": "2013-07-29 08:42:42",
"fact_cache_error": null,
"fact_cache_hit": true,
"header_info": null,
"name": null,
"report_size": "5871",
"row_count": "33",
"url": "report-download?id=278641c223bc4e4d63df9e83d8fcb4e6"
},
"status": "OK"
}
}

JSON 的响应部分对于一堆响应类型是通用的。此 JSON 的报告部分仅适用于此响应。因此,我创建了一个 Response 类,如下所示,其中包含 getter 和 setter(为简洁起见,此处未包括 getter 和 setter):

@JsonRootName(value = "response")
public class Response implements Serializable {

private static final long serialVersionUID = -2597493920293381637L;

@JsonProperty(value = "error")
private String error;
@JsonProperty(value = "error_code")
private String errorCode;
@JsonProperty(value = "error_id")
private String errorId;
@JsonProperty(value = "error_description")
private String errorDescription;
@JsonProperty(value = "method")
private String method;
@JsonProperty(value = "service")
private String service;
@JsonProperty(value = "status")
private String status;
@JsonProperty(value = "execution_status")
private String executionStatus;
}

然后,我创建了一个 Report 类,其中包含报告元素中的字段,如下所示。 ReportResponse 类将从 Response 类扩展(再次为简洁起见不包括 getter 和 setter):

public class ReportResponse extends Response {

private static final long serialVersionUID = 4950819240030644407L;

@JsonProperty(value = "cache_hit")
private Boolean cacheHit;
@JsonProperty(value = "created_on")
private Timestamp createdOn;
@JsonProperty(value = "fact_cache_error")
private String factCacheError;
@JsonProperty(value = "fact_cache_hit")
private Boolean factCacheHit;
@JsonProperty(value = "header_info")
private String headerInfo;
@JsonProperty(value = "json_request")
private String jsonRequest;
@JsonProperty(value = "name")
private String name;
@JsonProperty(value = "report_size")
private Integer reportSize;
@JsonProperty(value = "row_count")
private Integer rowCount;
@JsonProperty(value = "url")
private String url;
}

现在,当我使用 ObjectMapper 映射到 ReportResponse 对象时,出现以下错误:

String jsonString = "{\"response\": {\"execution_status\": \"ready\", \"report\":   {\"cache_hit\": true, \"created_on\": \"2013-07-29 09:53:44\", \"fact_cache_error\": null, \"fact_cache_hit\": false, \"header_info\": null, \"name\": null, \"report_size\": \"5871\", \"row_count\": \"33\", \"url\": \"report-download?id=2ff62c07fc3653b68f2073e7c1aa0517\"}, \"status\": \"OK\"}}";
ObjectMapper mapper = new ObjectMapper();
ReportResponse reportResponse = mapper.readValue(jsonString, ReportResponse.class);

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "report"

我知道我可以创建一个单独的 Report 类,然后使用 @JsonProperty 注释将其嵌入到 ReportResponse 中。有没有一种方法可以避免这种情况,并使用将其映射到 JSON 中的“报告”元素的注释来标记 ReportResponse 类?

最佳答案

目前还没有可以处理这种情况的注解。 There is a ticket requesting this feature.

Here is a brief statement from one of the owners regarding this topic.

引用上述声明:

Tatu Saloranta: "… @JsonProperty does not support transformations, since the data binding is based on incremental parsing and does not have access to full tree representation. Supporting @JsonUnwrapped was non-trivial, but doable; and thus converse ("@JsonWrapped") would be doable, theoretically speaking. Just plenty of work. …"

关于java - Jackson - 反序列化嵌套的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com