gpt4 book ai didi

java - 为什么我多次获取数据?

转载 作者:行者123 更新时间:2023-11-30 07:40:10 26 4
gpt4 key购买 nike

我正在使用 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;
}
}

子类.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}
]
}
]
}]
};

最佳答案

您正在重用列表 aChild1aChild2

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

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

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 时,我期望的是a child,而不是 child 的列表。当我阅读 Children 时,我希望得到一个children 的列表,而不是一个类。

最后但同样重要的是,考虑类的用途。它们描述属于同一类的对象的语义和行为。您所有的 Children 类都做同样的事情。有一个类 Children1 和一个类 Children2 等是没有意义的。只需创建一个名为 Child 的类。总是试着想想现实。当我们谈论人类 child 时,您会说“这是一个 child ”还是“这是一个 child 21984305”,因为以前有多少 parent ?不,当然不。您可以对对象进行编号,但对类进行编号是没有意义的。 child 在树中下 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);

关于java - 为什么我多次获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com