少林

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

转载自: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)

 

posted on 2015-09-14 16:48  Stanley_yu  阅读(695)  评论(0编辑  收藏  举报