轉: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());
引用是通過”
“
使用fastjson 可以避免以上的空對象數據出現!