0

How can I create this json content recursively using a java object and Jackson ? :

{
    "children": [
        {
            "children": [
                {
                    "name": "cluster",
                    "children": [
                        {
                            "name": "AgglomerativeCluster",
                            "size": 3938
                        },
                        {
                            "name": "TestCLuster",
                            "size": 3938
                        }
                    ]
                }
            ],
            "name": "analytics"
        }
    ],
    "name": "flare"
}

Here is my object structure so far, a parent named DendogramVO and a child class named Children which also has a children List :

import java.util.List;

public class DendogramVO {
    private List<Children> children;
    private String name;

    public List<Children> getChildren() {
        return this.children;
    }

    public void setChildren(List<Children> children) {
        this.children = children;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

import java.util.List;

public class Children {

    private List<Children> children;
    private String name;

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

    public void setChildren(List<Children> children) {
        this.children = children;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

1 Answer 1

2

Note that your JSON contains unknown properties. You need to configure the ObjectMapper not to fail when it comes across them (or add them to your class)

String s="{ \"children\": [ { \"children\": [ { \"name\": \"cluster\", \"children\": [ { \"name\": \"AgglomerativeCluster\", \"size\": 3938 }, { \"name\": \"TestCLuster\", \"size\": 3938 } ] } ], \"name\": \"analytics\" } ], \"name\": \"flare\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Children children = mapper.readValue(s, Children.class);

System.out.println(children.getChildren());

with a simple Children#toString() prints

[[children = [[children = [[children = null, name = AgglomerativeCluster], [children = null, name = TestCLuster]], name = cluster]], name = analytics]]
Sign up to request clarification or add additional context in comments.

12 Comments

Children#toString() does not match the json tree structure that I have posted in question?
@user470184 Yeah it does. The most nested json object contains a json array that contains two json objects. It has the name cluster. The object that contains it has a name analytics. And that object is children. If you do children.getName() in the code above, it will print flare, which is the name of the root json object.
have you tried your code , mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); causes compiler error : "The method configure(SerializationConfig.Feature, boolean) in the type ObjectMapper is not applicable for the arguments (DeserializationFeature, boolean)"
@user470184 Your ObjectMapper should be in package com.fasterxml.jackson.databind.
@user470184 There might be a SerializationFeature for that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.