fastjson tostring ref 问题

简介:    解决方法:SerializerFeature.DisableCircularReferenceDetect JSON.toJSONString(result,SerializerFeature.

 

 解决方法: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/



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

目录
相关文章
|
12月前
|
JSON 数据格式
【异常】com.alibaba.fastjson.JSONException: unclosed string : U
【异常】com.alibaba.fastjson.JSONException: unclosed string : U
880 0
|
JSON fastjson Java
fastjson:对象转化成json出现$ref
fastjson:对象转化成json出现$ref
1203 0
|
2月前
|
JSON 前端开发 JavaScript
JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value
这篇文章讨论了前端Vue应用向后端Spring Boot服务传输数据时发生的类型不匹配问题,即后端期望接收的字段类型为`int`,而前端实际传输的类型为`Boolean`,导致无法反序列化的问题,并提供了问题的诊断和解决方案。
JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value
|
Java Spring 容器
No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies
No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies
898 0
No qualifying bean of type ‘java.lang.String‘ available: expected at least 1 bean which qualifies
|
JSON 前端开发 数据格式
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `object` out of START_ARRAY token
讲述如何处理 org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `object` out of START_ARRAY token的问题
 org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `object` out of START_ARRAY token
|
JSON NoSQL Redis
Redis反序列化错误Could not read JSON: Cannot construct instance of `java.util.ArrayList$SubList`
Redis反序列化错误Could not read JSON: Cannot construct instance of java.util.ArrayList$SubList
1012 1
Redis反序列化错误Could not read JSON: Cannot construct instance of `java.util.ArrayList$SubList`
|
JSON 数据格式 Python
TypeError: Object of type 'datetime' is not JSON serializable
TypeError: Object of type 'datetime' is not JSON serializable
172 0
|
JSON fastjson 数据格式
利用fastjson对json转map的操作
String str = "{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}"; //第一种方式 Map maps = (Map)JSON.
11720 1
|
存储 JSON 关系型数据库
com.alibaba.fastjson 对象转json剔除字段
com.alibaba.fastjson在对象转json时剔除不需要的字段
com.alibaba.fastjson 对象转json剔除字段