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

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

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

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

JSON.toJSONString( yourObject, SerializerFeature.DisableCircularReferenceDetect);  


    循环引用 很多场景中,我们需要序列化的对象中存在循环引用,在许多的json库中,这会导致stackoverflow。在功能强大的fastjson中,你不需要担心这个问题。例如:

  


 

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)

猜你喜欢

转载自www.cnblogs.com/kongxc/p/9077931.html