Why I am getting data multiple times?

Rockers :

I am creating json using java pojo class. In this only when i am looping through some value it prints that value multiple times in json.

Here is my java class :-

public class App {

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        MyPojo myPojo = createDummy();

        String jsonString;
        try 
        {
            jsonString = mapper.writeValueAsString(myPojo);
            System.out.println(jsonString);
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

    private static MyPojo createDummy() 
    {
        MyPojo myPojo = new MyPojo();
        myPojo.setName("State");

        Children child;
        ArrayList<Children> aChild = new ArrayList<>();

        Children1 child1;
        ArrayList<Children1> aChild1 = new ArrayList<>();

        Children2 child2 = null;
        ArrayList<Children2> aChild2 = new ArrayList<>();
        for(int i=0; i<3; i++)
        {
            child = new Children();
            child.setName("Bihar");

            for(int j=0; j<6; j++)
            {
                child1 = new Children1();
                child1.setName("Patna");

                for(int k=0; k<2; k++)
                {
                    child2 = new Children2();
                    child2.setName("CSE");
                    child2.setSize(4);

                    aChild2.add(child2);
                }
                child1.setChildren2(aChild2);
                aChild2.remove(child2);
                aChild1.add(child1);
            }
            child.setChildren1(aChild1);
            aChild.add(child);
        }
        myPojo.setChildren(aChild);

        return myPojo;
    }
  }

MyPojo.java class :-

public class MyPojo 
{
    private String name;
    private ArrayList<Children> children;

    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    public ArrayList<Children> getChildren() 
    {
        return children;
    }
    public void setChildren(ArrayList<Children> children) 
    {
        this.children = children;
    }   
}

Children.java

public class Children {
    private String name;
    private ArrayList<Children1> children1;

    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    public ArrayList<Children1> getChildren1() 
    {
        return children1;
    }
    public void setChildren1(ArrayList<Children1> children1) 
    {
        this.children1 = children1;
    }

}

Children1.java

public class Children1 {
    private String name;
    private ArrayList<Children2> children2;

    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    public ArrayList<Children2> getChildren2() 
    {
        return children2;
    }
    public void setChildren2(ArrayList<Children2> children2) 
    {
        this.children2 = children2;
    }

}

Children2.java

public class Children2 {
    private String name;
    private int size;

    public String getName() 
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    public int getSize() 
    {
        return size;
    }
    public void setSize(int size) 
    {
        this.size = size;
    }
}

I want my json output like this :- How to get this?

{
    "name": "State", "children": 
    [{
        "name": "Bihar",
        "children": 
        [
        {"name": "Patna",
         "children":
             [
              {"name": "CSE", "size": 0},
              {"name": "CSE", "size": 0}
             ]
        }, 
        {"name": "Patna",
         "children":
             [
              {"name": "CSE", "size": 0},
              {"name": "CSE", "size": 0}
             ]
        }
        ]
    },
     {
        "name": "Bihar",
        "children": 
        [
        {"name": "Patna",
         "children":
             [
              {"name": "CSE", "size": 0},
              {"name": "CSE", "size": 0}
             ]
        }, 
        {"name": "Patna",
         "children":
             [
              {"name": "CSE", "size": 0},
              {"name": "CSE", "size": 0}
             ]
        }
        ]
    }, 
    {
        "name": "Bihar",
        "children": 
        [
        {"name": "Patna",
         "children":
             [
              {"name": "CSE", "size": 0},
              {"name": "CSE", "size": 0}
             ]
        }, 
        {"name": "Patna",
         "children":
             [
              {"name": "CSE", "size": 0},
              {"name": "CSE", "size": 0}
             ]
        }
        ]
    }]
};
Max Vollmer :

You are reusing the lists aChild1 and aChild2.

You must understand that assigning an object to a reference in Java does not create a copy. It just makes that reference reference that object (hence the name reference).

You need to move those lists into your loop, so that you create a new list for every child. In general you should always declare variables and create objects where you need them, not earlier. That makes your code much more readable and prevents bugs as you encountered now:

ArrayList<Children> aChild = new ArrayList<>();
for(int i=0; i<3; i++)
{
    Children child = new Children();
    child.setName("Bihar");

    ArrayList<Children1> aChild1 = new ArrayList<>();
    for(int j=0; j<6; j++)
    {
        Children1 child1 = new Children1();
        child1.setName("Patna");

        ArrayList<Children2> aChild2 = new ArrayList<>();
        for(int k=0; k<2; k++)
        {
            Children2 child2 = new Children2();
            child2.setName("CSE");
            child2.setSize(4);

            aChild2.add(child2);
        }
        child1.setChildren2(aChild2);
        aChild1.add(child1);
    }
    child.setChildren1(aChild1);
    aChild.add(child);
}
myPojo.setChildren(aChild);

Aside from that you really need to reconsider how you name your classes and objects. A list called aChild makes no sense. It's a list after all and should be called children. And on the other hand a class called Children makes no sense, it's one class, so it should be called Child. Make your code talk. When I read aChild I expect a child, not a list of children. When I read Children I expect a list of children, not a class.

And last but not least, consider what classes are for. They describe the semantics and behavior of objects that are, well, of the same class. All your Children classes do the exact same thing. It makes no sense to have a class Children1 and a class Children2 etc. Just make one class called Child. Always try to think of reality. When we talk about human children, would you say "this is a child" or would you say "this is a child21984305" because that's how many parents came before? No, of course not. You can number objects, but numbering classes makes no sense. The information that a child is 2 levels down in your tree is an information belonging to the object, not an information belonging to the class.

Here's a small code example of the same code using proper classes and names. You might notice how much more readable and understandable the code suddenly is:

ArrayList<Child> children = new ArrayList<>();
for(int i=0; i<3; i++)
{
    Child child = new Child();
    child.setName("Bihar");
    children.add(child);

    ArrayList<Child> innerchildren = new ArrayList<>();
    for(int j=0; j<6; j++)
    {
        Child innerchild = new Child();
        innerchild.setName("Patna");
        innerchildren.add(innerchild);

        ArrayList<Child> innermostchildren = new ArrayList<>();
        for(int k=0; k<2; k++)
        {
            SizableChild innermostchild = new SizableChild();   // SizableChild extends Child and adds the methods setSize and getSize
            innermostchild.setName("CSE");
            innermostchild.setSize(4);

            innermostchildren.add(innermostchild);
        }
        innerchild.setChildren(innermostchildren);
    }
    child.setChildren(innerchildren);
}
myPojo.setChildren(children);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308274&siteId=1