fastjson生成JSON字符串的时候出现$ref [转]

转载自:http://wuzhuti.cn/201426!826!05!130202.html


      可以通过选项 DisableCircularReferenceDetect来禁止循环引用检测:

JSON.toJSONString( yourObject, SerializerFeature.DisableCircularReferenceDetect);
A a = new A();
B b = new B(a);
a.setB(b);

String text = JSON.toJSONString(a); // {"b":{"a":{"$ref":".."}}}
A a1 = JSON.parseObject(text, A.class);
Assert.assertTrue(a1 == a1.getB().getA());

引用是通过"$ref"来表示的
          引用 描述
          "$ref":".." 上一级
          "$ref":"@" 当前对象,也就是自引用
          "$ref":"$" 根对象
          "$ref":"$.children.0" 基于路径的引用,相当于 root.getChildren().get(0)

原文地址:https://www.cnblogs.com/yushouling/p/4807603.html