当前位置: 首页 > 知识库问答 >
问题:

Jackson-反序列化嵌套JSON

岳浩
2023-03-14
{
  "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"
  }
}
@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;
}
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;
}
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中。有没有一种方法可以避免这种情况,并用一个注释标记ReportResponse类,将它映射到JSON中的“Report”元素?

共有1个答案

夹谷星河
2023-03-14

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

这里有一个简短的声明,从业主之一关于这个主题。

引用上述声明:

 类似资料:
  • 我正在创建一个具有嵌套列表的API。Jackson似乎是一个创建对象的好工具,但我不太清楚如何嵌套列表,我想知道这是否可能。 我的对象看起来像这样。 我希望有一种方法可以将其映射到json,看起来像: 我们希望能够做到这一点,以便我们可以将属性添加到列表中。

  • 我有以下xml 我需要将其反序列化为以下POJO: 这里的问题是被包装在元素中

  • 我正在尝试使用Jakson反序列化一个嵌套的多态类型。也就是说,我的顶级类型引用了另一个多态类型,该类型最终由不抽象的类扩展。这不起作用,它会抛出一个异常。 下面是我尝试做的一个简化的例子。 我得到了关于抽象类型的标准异常。 让我解释一下我的用例。我有一个描述数据工作流的Json文档。我在“Level One”有一个抽象类型,描述对单个值的操作。我派生了一堆不是抽象的类,它们实现了公共操作(我用@

  • 问题内容: 我有一个Vendor对象,可以从一个单独的“ vendor” json序列中反序列化,但是我想将此序列反序列化为一个,我只是想不出如何让Jackson合作。有小费吗? 问题答案: 您的数据存在问题,因为您的数组中有内部 包装 对象。想必你的对象被设计成手柄,,,但每次的多个对象也都包裹在一个对象与单一属性。 我假设您正在使用Jackson 数据绑定 模型。 如果是这样,那么有两件事要考

  • 但它返回一个id为空且产品为空的对象。当然,我不需要为这个简单的操作编写自定义的吗?

  • 注意:和是第三方类。所以我不能注释这些。