杰克逊 - 反序列化嵌套的JSON [英] Jackson - Deserialize nested JSON

查看:116
本文介绍了杰克逊 - 反序列化嵌套的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSON string which will be of the following format:

{
  "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):

The response part of the JSON is common for a bunch of response types. The report part of this JSON holds good only for this response. So I had created a Response class as shown below with getters and setters (have not included the getters and setters here for brevity):

@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类,其中包含report元素中的字段下面。 ReportResponse类将从Response类扩展(为简洁起见,不包括getter和setter):

And then, I created a Report class with the fields in the report element as below. The ReportResponse class will extend from the Response class (again the getters and setters are not included for brevity):

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对象时,我得到了以下错误:

Now when I use the ObjectMapper to map to the ReportResponse object, I get the following error:

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 anotation将其嵌入到ReportResponse中。有没有办法可以避免这种情况,并使用注释将ReportResponse类标记为JSON中的report元素?

I know that I can create a separate Report class and then embed it in the ReportResponse with the @JsonProperty anotation. Is there a way I can avoid that and mark the ReportResponse class with an annotation which would map it to the "report" element in the JSON?

推荐答案

没有可以处理这种情况的注释。 有一张票要求此功能。

There is no annotation which could handle this case yet. There is a ticket requesting this feature.

以下是一份简短声明关于这个主题的所有者。

从上述声明引用:


Tatu Saloranta:... @JsonProperty不支持转换,因为数据绑定基于增量解析,并且无法访问完整的树表示。支持@JsonUnwrapped非常重要,但可行;因而相反(从理论上讲,@JsonWrapped是可行的。只需要做很多工作......

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. …"

这篇关于杰克逊 - 反序列化嵌套的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆