简体   繁体   English

Jackson继承-使用@JsonProperty在子类中解析嵌套的JSON

[英]Jackson inheritance - parsing nested JSON in a subclass using @JsonProperty

I am trying to use Jackson to parse network responses. 我正在尝试使用Jackson来解析网络响应。 All responses follow this general format: 所有回复均遵循以下一般格式:

{
  "status": {
    "code": 0,
    "message": "ok",
    "version": 1
  },
  "data": {
    "accountId": 1111,
    "handle": "mark",
    "stats": {
       "wins": 11,
       "losses": 43
    }
  }
}

The "status" node is shared among all objects, while the "data" node will vary from response to response. “状态”节点在所有对象之间共享,而“数据”节点将因响应而异。 My thought was to create a NetworkResponse class that handles parsing the "status" node and then create many objects that inherit from NetworkResponse that parse the different NetworkResponses I'm getting. 我的想法是创建一个处理“状态”节点解析的NetworkResponse类,然后创建许多从NetworkResponse继承的对象,这些对象解析我得到的不同NetworkResponses。

So far, I made this for the base class: 到目前为止,我是针对基类制作的:

public class NetworkResponse {
    @JsonProperty("status") protected StatusNode statusNode;

    public NetworkResponse() { }
}

And then a subclass that would parse the above network response: 然后是一个子类,它将解析上述网络响应:

public class PlayerInfoResponse extends NetworkResponse {
    @JsonProperty("accountId") private String accountId;
    @JsonProperty("handle") private String handle;
    @JsonProperty("stats") private PlayerStats playerStats;

    public PlayerInfoResponse() {}
}

The problem here is neither the superclass nor child class acknowledge the "data" node. 这里的问题是父类或子类都不承认“数据”节点。 Now, I could implement the child class like this: 现在,我可以像这样实现子类:

public class PlayerInfoResponse extends NetworkResponse {
    private String accountId;
    private String handle;
    private PlayerStats playerStats;

    @JsonProperty("data") public JsonNode dataNode;
    public void setDataNode(JsonNode dataNode) {
            // parse out data manually 
    }

    public PlayerInfoResponse() {}
}

This works, but I'd like to use @JsonProperty to keep every tidier. 这行得通,但我想使用@JsonProperty保持所有整理。 Is there anyway to get Jackson to treat the "data" node as the node it should be using when parsing? 无论如何,有没有让杰克逊将“数据”节点视为解析时应使用的节点? Here's the code I'm using to do the mapping: 这是我用来进行映射的代码:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
PlayerInfoResponse info = objectMapper.readValue(jsonResponse, PlayerInfoResponse.class);

Thanks! 谢谢!

A couple of ways to go about this. 有两种方法可以解决此问题。

Simplest way is just restructure your response. 最简单的方法就是重组您的响应。

public abstract class NetworkResponse {
  @JsonProperty("status") private StatusNode statusNode;
  // getters and setters
}

public class PlayerInfoResponse extends NetworkResponse {
    private PlayerData data;

    public static class PlayerData {
      private String accountId;
      private String handle;
      @JsonProperty("stats") private PlayerStats playerStats;

      // getters and setters
    }
    // getters and setters
 }  

If you noticed, you a json field data not defined, in your player response object 如果您注意到,您在播放器响应对象中未定义json字段数据

Also, @JsonProperty just lets you rename the mapping between a java bean field and it's json counterpart. 另外,@ JsonProperty仅允许您重命名Java bean字段与其json对应项之间的映射。

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

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