fastjson出现$ref循环引用

参考:https://www.cnblogs.com/taoxw/p/16589878.html

问题描述:

使用fastjson将对象序列化为字符串时,有时候会出现$ref的情况,这种情况叫循环引用(CircularReference)。如下图所示:

 

为什么会出现$ref

.这是因为,对象与对象之间是存在关系引用。

解决办法

使用序列化特性类(SerializerFeature),代码如下

response.setCharacterEncoding("utf-8");

response.setContentType("application/json; charset=utf-8");

String jsonString = JSON.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.PrettyFormat);

猜你喜欢

转载自blog.csdn.net/banzhengyu/article/details/130021715