阅读 36

fastjson一个集合被多次引用的问题

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;
    }
    
}
复制代码

View Code

public class B {
    private List<D> dlist;

    public List<D> getDlist() {
        return dlist;
    }

    public void setDlist(List<D> dlist) {
        this.dlist = dlist;
    }
    
}
复制代码

View Code

public class C {
    private List<D> dlist;

    public List<D> getDlist() {
        return dlist;
    }

    public void setDlist(List<D> dlist) {
        this.dlist = dlist;
    }
    
}
复制代码

View Code

public class D {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    
}
复制代码

View Code

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);
复制代码

View Code

 

文章分类
后端
文章标签