fastjson tostring ref 问题

解决方法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","last":{"description":"hello","name":"baoyou"}}

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

个人主页http://knight-black-bob.iteye.com/


谢谢您的赞助,我会做的更好!

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...