最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jackson - Deserializing heavily nested JSON to Java Object - Stack Overflow

programmeradmin6浏览0评论

Below is the JSON which I need to convert to Java Object.

{
    "example": {
        "rule_string": "Elected.Benefit Area~ ++~ Is Eq ual To~ ++~ \"MEDICAL\"~ And~ Ends with the Text~ (~ Elected.Benefit Plan Option~ ,~ \"PPO\"~ )~",
        "rule_source": "Elected.Benefit Area~ Is Eq ual To~ \"MEDICAL\"~ ~And~ Ends with the Text~ (~ Elected.Benefit Plan Option~ ,~ \"PPO\"~~ )~ ~ ",
        "rule_type": "if",
        "ast": {
            "data": "if_statement",
            "children": [
                {
                    "data": "and_test",
                    "children": [
                        {
                            "data": "concat",
                            "children": [
                                {
                                    "data": "field",
                                    "children": [
                                        "Elected.Benefit Area"
                                    ],
                                    "_meta": null
                                },
                                {
                                    "data": "concat",
                                    "children": [
                                        {
                                            "data": "field",
                                            "children": [
                                                "Is Eq ual To"
                                            ],
                                            "_meta": null
                                        },
                                        {
                                            "data": "string",
                                            "children": [
                                                "\"MEDICAL\""
                                            ],
                                            "_meta": null
                                        }
                                    ],
                                    "_meta": null
                                }
                            ],
                            "_meta": null
                        },
                        {
                            "data": "function",
                            "children": [
                                {
                                    "data": "function_name",
                                    "children": [
                                        "Ends with the Text"
                                    ],
                                    "_meta": null
                                },
                                {
                                    "data": "function_args",
                                    "children": [
                                        {
                                            "data": "field",
                                            "children": [
                                                "Elected.Benefit Plan Option"
                                            ],
                                            "_meta": null
                                        },
                                        {
                                            "data": "string",
                                            "children": [
                                                "\"PPO\""
                                            ],
                                            "_meta": null
                                        }
                                    ],
                                    "_meta": null
                                }
                            ],
                            "_meta": null
                        }
                    ],
                    "_meta": null
                }
            ],
            "_meta": null
        },
        "valid": true,
        "parse_error": null
    }
}

I used / to generate the classes which are as below. I have removed setters & getters in the snippets below. Each of the setters & getters have the @JsonProperty annotation.

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "rule_string",
    "rule_source",
    "rule_type",
    "ast",
    "valid",
    "parse_error"
})
public class Example {

    @JsonProperty("rule_string")
    private String ruleString;
    @JsonProperty("rule_source")
    private String ruleSource;
    @JsonProperty("rule_type")
    private String ruleType;
    @JsonProperty("ast")
    private Ast ast;
    @JsonProperty("valid")
    private Boolean valid;
    @JsonProperty("parse_error")
    private Object parseError;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Ast {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List<Child> children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Child {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List<Child__1> children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Child__1 {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List<Child__2> children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "data",
    "children",
    "_meta"
})
public class Child__2 {

    @JsonProperty("data")
    private String data;
    @JsonProperty("children")
    private List<String> children;
    @JsonProperty("_meta")
    private Object meta;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();
}

I suspect the issue lies in the inner most "chidren" node which is an array of Strings where as the other "children" nodes are arrays of objects. Will it be possible to parse this JSON into POJOs using Jackson?

发布评论

评论列表(0)

  1. 暂无评论