微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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/


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐