How to build model my application which adds child elements dynamically in spring boot

142 Views Asked by At

I have to build java objects with following json structure.

sample:

{
  "parent":""
  "children":[
      {
      "parent":"abc"
        "children":[
           parent:"ccd"{
              "children":[]
           }
           
         ]

      }
       
   }
}

I have json like this where a parent can have child object and this child object act as parent to another child object and the chain can increase dynamically . How can we model object so that I can form json object like above from my API.

Thanks in advance

1

There are 1 best solutions below

0
Ewan Arends On

One way to do this would be to store the same Object type as your child, so in this case you would get something like this;

(...)
public class SampleObject {

    String parent;

    SampleObject[] children;

    public SampleObject(String parent, SampleObject... children) {
        this.parent = parent;
        this.children = children;
    }

    public SampleObject() {
        this("")
    }
(...)
}