加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

fastjson把对象转化成json避免$ref

发布时间:2020-12-16 19:43:05 所属栏目:百科 来源:网络整理
导读:转:http://blog.csdn.net/mephistodemon1/article/details/19118493 DisableCircularReferenceDetect来禁止循环引用检测: JSON.toJSONString(...,SerializerFeature.DisableCircularReferenceDetect) 当进行toJSONString的时候,默认如果重用对象的话,会

转:http://blog.csdn.net/mephistodemon1/article/details/19118493

DisableCircularReferenceDetect来禁止循环引用检测:

JSON.toJSONString(...,SerializerFeature.DisableCircularReferenceDetect)

当进行toJSONString的时候,默认如果重用对象的话,会使用引用的方式进行引用对象。
"颜色": [
      {
        "$ref": "$.itemSkuList[0].itemSpecificationList[0]"
      },{
        "$ref": "$.itemSkuList[1].itemSpecificationList[0]"
      }
    ]

循环引用

很多场景中,我们需要序列化的对象中存在循环引用,在许多的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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读