简体   繁体   English

如何使用杰克逊API将高JSON层次结构复制到Java对象中?

[英]How to replicate high json hierarchy into java objects using jackson api?

I want to write a code in JAVA using JACKSON that allows to display a high hierarchy in JSON like the following extract. 我想使用JACKSON在JAVA中编写一个代码,该代码允许在JSON中显示较高的层次结构,如以下摘录所示。 In addition, the hierarchy must be structered in a way that the resulting json application depends on conditions "validFrom" and "validTo". 此外,必须以一种方式构造层次结构,使所得的json应用程序取决于条件“ validFrom”和“ validTo”。

In the example below all dates for "validFrom" and "validTo" are the same but in practice the dates could vary, ie each child element could have several different validity periods: 在下面的示例中,“ validFrom”和“ validTo”的所有日期都相同,但实际上日期可能有所不同,即每个子元素可以具有几个不同的有效期:

Eg possible dates for "DE": 例如“ DE”的可能日期:
"validFrom": ["2000-01-31", "1995-01-31"] “ validFrom”:[“ 2000-01-31”,“ 1995-01-31”]
"validTo": ["2099-01-31", "2000-01-30"] “ validTo”:[“ 2099-01-31”,“ 2000-01-30”]

So, for the combination of "validFrom"="1995-01-31" and "validTo"="2000-01-30" the child element "DE" hypothetically should belong to the parent "Z9" (not seen in the json example provided below!) but for the combination of "validFrom"="2000-01-31" and "validTo"="2099-01-31" it should belong to the parent "U6" (like in the json example below). 因此,对于“ validFrom” =“ 1995-01-31”和“ validTo” =“ 2000-01-30”的组合,假设子元素“ DE”应该属于父元素“ Z9”(在json中未看到)下面提供的示例!),但对于“ validFrom” =“ 2000-01-31”和“ validTo” =“ 2099-01-31”的组合,它应属于父级“ U6”(例如下面的json示例) 。

Ie depending on attributes "validFrom" and "validTo" the child element could belong to either one parent or another parent (in the example below "DE" only belongs to parent "U6" but the question is to implement such conditions into java code in a way that there is a differentiation possible). 即根据属性“ validFrom”和“ validTo”,子元素可以属于一个父对象或另一个父对象(在下面的示例中,“ DE”仅属于父“ U6”,但问题是要在Java代码中将此类条件实现为一种可能与众不同的方式)。 Whithin one hierarchy level a child element should belong only to one parent. Whithin在一个层次结构中,一个子元素应该仅属于一个父元素。

What does the code in java have to look like in order to write this structure in json using jackson? 为了使用杰克逊在json中编写此结构,java中的代码必须是什么样的?

{
"HierarchicalCode":[
{
    "code": "A1",
    "description": "Welt",
    "validFrom": "2000-01-31",
    "validTo": "2099-01-31",

    "children":[
        {
        "code": "U6",
        "description": "Inland",
        "validFrom": "2000-01-31",
        "validTo": "2099-01-31",

            "children":[
            {
            "code": "DE",
            "description": "Deutschland",
            "validFrom": "2000-01-31",
            "validTo": "2099-01-31"
            }
            ]
        },
        {
        "code": "Z9",
        "description": "Ausland",
        "validFrom": "2000-01-31",
        "validTo": "2099-01-31"

        }
    ]
}



]
}

I didn't quite get that logic based on validFrom and validTo dates. 我不太了解基于有效日期和有效日期的逻辑。

First, you need to construct your model, second, you have to serialize it using object mapper. 首先,您需要构建模型,其次,您必须使用对象映射器对其进行序列化。 Example: 例:

public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    HierarchicalCode hierarchicalCode = objectMapper.readValue(s, HierarchicalCode.class);
    // we constructed our hierarchical code here somehow, now serialize
    String result = objectMapper.writeValueAsString(hierarchicalCode);
    System.out.println(result);
}

private static class HierarchicalCode {

    @JsonProperty("HierarchicalCode")
    private List<HierarchicalElement> hierarchicalCode;

    public List<HierarchicalElement> getHierarchicalCode() {
        return hierarchicalCode;
    }
}

private static class HierarchicalElement {

    private String code;
    private String description;
    private Timestamp validFrom;
    private Timestamp validTo;
    private List<HierarchicalElement> children;

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public Timestamp getValidFrom() {
        return validFrom;
    }

    public Timestamp getValidTo() {
        return validTo;
    }

    public List<HierarchicalElement> getChildren() {
        return children;
    }
}

Hope that helps. 希望能有所帮助。

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

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