public class A { private B b; private C c; public B getB() { return b; } public void setB(B b) { this.b = b; } public C getC() { return c; } public void setC(C c) { this.c = c; } }
public class B { private List<D> dlist; public List<D> getDlist() { return dlist; } public void setDlist(List<D> dlist) { this.dlist = dlist; } }
public class C { private List<D> dlist; public List<D> getDlist() { return dlist; } public void setDlist(List<D> dlist) { this.dlist = dlist; } }
public class D { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
D d = new D(); d.setName("张三"); ArrayList<D> dList = new ArrayList<>(); dList.add(d); B b = new B(); b.setDlist(dList); C c = new C(); c.setDlist(dList); A a = new A(); a.setB(b); a.setC(c); String jsonString = JSON.toJSONString(a,SerializerFeature.DisableCircularReferenceDetect); System.out.println(jsonString);