解决方法:SerializerFeature.DisableCircularReferenceDetect
JSON.toJSONString(result,SerializerFeature.DisableCircularReferenceDetect)
/*
* Project: DemoJava
*
* File Created at 2017年5月23日
*
* Copyright 2016 CMCC Corporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* curiousby Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license.
*/
package com.curiousby.fastjson;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* @Type FastjsonTest.java
* @Desc
* @author baoyou curiousby@163.com
* @date 2017年5月23日 上午11:01:21
* @version
*/
public class FastjsonTest {
public static void main(String[] args) {
FastJsonEntity entity = new FastJsonEntity(1,"baoyou","hello");
FastResultEnity result = new FastResultEnity();
result.setLast(entity);
List<FastJsonEntity> list = new ArrayList<>();
list.add(entity);
result.setHistory(list);
System.out.println(JSON.toJSONString(result));
System.out.println(JSON.toJSONString(result,SerializerFeature.DisableCircularReferenceDetect));
}
}
class FastResultEnity{
private FastJsonEntity last;
private List<FastJsonEntity> history;
public FastJsonEntity getLast() {
return last;
}
public List<FastJsonEntity> getHistory() {
return history;
}
public void setLast(FastJsonEntity last) {
this.last = last;
}
public void setHistory(List<FastJsonEntity> history) {
this.history = history;
}
}
class FastJsonEntity{
private int id;
private String name;
private String description;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public FastJsonEntity(){}
public FastJsonEntity(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
}
/**
* Revision history
* -------------------------------------------------------------------------
*
* Date Author Note
* -------------------------------------------------------------------------
* 2017年5月23日 baoyou curiousby@163.com creat
*/
test
{"history":[{"description":"hello","id":1,"name":"baoyou"}],"last":{"$ref":"$.history[0]"}}
{"history":[{"description":"hello","id":1,"name":"baoyou"}],"last":{"description":"hello","id":1,"name":"baoyou"}}
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!