fastjson中$ref对象重复引用问题解决方法
来源:互联网 发布:小米盒子软件大全 编辑:程序博客网 时间:2024/06/08 19:48
import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;/** * fastjson中$ref对象重复引用问题 * * 介绍: * FastJson提供了SerializerFeature.DisableCircularReferenceDetect这个序列化选项,用来关闭引用检测。 * 关闭引用检测后,重复引用对象时就不会被$ref代替,但是在循环引用时也会导致StackOverflowError异常。 * * 用法: * JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect); */public class TestFastJson { List<Person> list = new ArrayList<Person>(); Person p = new Person("ZhangSan", 23); /** * 测试main */ public static void main(String[] args) { // new TestFastJson().test1(); new TestFastJson().test2(); } /** * 未关闭引用检测,转化字符串时会出现$ref */ public void test1() { list.add(p); list.add(p); String jsonStr = JSON.toJSONString(list); System.out.println(jsonStr); // [{"age":23,"name":"ZhangSan"},{"$ref":"$[0]"}] } /** * 关闭引用检测后,$ref的问题就会被解决 */ public void test2() { list.add(p); list.add(p); String jsonStr = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect); System.out.println(jsonStr); // [{"age":23,"name":"ZhangSan"},{"age":23,"name":"ZhangSan"}] }}class Person { private String name; private int age; // 构造函数 public Person(String name, int age) { super(); this.name = name; this.age = age; } // getters/setters(略)}
转载:http://www.cnblogs.com/zj0208/p/6196632.html
0 0
- fastjson中$ref对象重复引用问题解决方法
- 【FastJSON】解决FastJson中“$ref 循环引用”的问题
- 【FastJSON】解决FastJson中“$ref 循环引用”的问题
- 解决FastJson中“$ref 循环引用”的问题
- 【FastJSON】解决FastJson中“$ref 循环引用”的问题<三种方式对应不同需求>
- 解决fastjson内存对象重复/循环引用json错误
- 解决使用fastJson中出现$ref问题
- 解决FastJson的$ref循环引用检测的问题
- fastjson把对象转化成json避免$ref
- fastjson把对象转化成json避免$ref
- fastJson 解决 把对象转化成json避免$ref
- fastjson把对象转化成json避免$ref
- fastjson把对象转化成json避免$ref
- fastjson把对象转化成json避免$ref
- fastjson把对象转化成json避免$ref
- Spring中引用标签ref和属性ref
- 数组中取值不重复问题解决方法
- C#中引用类型和引用关键字ref 区别说明
- redis (Window下安装)
- sql规范
- 根据经纬度计算地图上两点的距离
- ES6中class原理,static属性方法,__proto__与prototype深入分析
- alignSelf
- fastjson中$ref对象重复引用问题解决方法
- 调试Devc++
- 接受一个日期的构造函数
- OpenXML SDK 读取WORD文档中的内容控件
- mysql 实现主从双向同步
- 揭开伪基站的神秘面纱
- JDBC浅析
- esxi
- 单例模式