为什么我要多次获取数据?

时间:2019-09-24 11:49:09

标签: java json

我正在使用java pojo类创建json。只有在我遍历某个值时,它才会在json中多次打印该值。

这是我的java类:-

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类:-

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;
    }
}

我想要这样的json输出:- 如何获得?

{
    "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}
             ]
        }
        ]
    }]
};

1 个答案:

答案 0 :(得分:3)

您正在重用列表aChild1aChild2

您必须了解,在Java中将对象分配给引用不会创建副本。只是使该引用引用了该对象(因此称为 reference )。

您需要将这些列表移入循环,以便为每个孩子创建一个新列表。通常,应该始终声明变量并在需要它们的地方创建对象,而不是更早。这使您的代码更具可读性,并避免了现在遇到的错误:

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);

除此之外,您确实需要重新考虑如何命名类和对象。名为aChild的列表没有任何意义。毕竟,这是一个列表,应称为children。另一方面,称为Children的类是没有意义的,它是一个类,因此应将其称为Child。让您的代码说话。当我阅读aChild时,我期望的是一个孩子,而不是孩子列表。当我阅读Children时,我希望得到一个孩子列表,而不是一个班级。

最后但并非最不重要的一点是,考虑要使用什么类。它们描述了属于同一类的对象的语义和行为。您所有的Children类都做同样的事情。拥有一个类Children1和一个类Children2等毫无意义。只需创建一个名为Child的类即可。始终尝试思考现实。当我们谈论人类孩子时,您会说“这是一个孩子” 还是会说“这是一个孩子21984305” ,因为那是多少父母来过的?不,当然不是。您可以为对象编号,但是编号类没有意义。在树中,一个孩子在2层以下的信息是属于该对象的信息,而不是属于该类的信息。

这是使用适当的类和名称的相同代码的小代码示例。您可能会注意到代码突然变得更具可读性和可读性:

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);