fastjson 如何去掉 $ref,使用方法,不讲原理

fastjson 是一个 不错的json格式化工具,

但是在使用时,如果 碰到统一地址对象引用,就会用$ref替代 。

怎么去掉ref呢,

这里 有两种方法

1. 使用 

SerializerFeature 订制 

如变代码引用 注释,部分


2.使用 

JSONObject.toJSON(map).toString()

多说一点,看下边  


String str ="testchar" ;

JSONObject.toJSONString(str) 返回   "\"testchat\"" ,多一套转移 引号
JSONObject.toJSON(str) 返回 "testchar"



这时不会把 文字转成 "\"testchat\'" ,'testchar'

package authdemo;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class TestMyTest {
	
	public static void main(String[] args) {
		Map<String,Object> map = new HashMap<String, Object>() ;
		Map<String,Object> inenermap = new HashMap<String, Object>() ;
		inenermap.put("key1", "12131") ;
		
		map.put("key3", "333") ;
		map.put("inmap", inenermap) ;
		map.put("in2", inenermap) ;		
		
//		SerializerFeature[] features = {
//			SerializerFeature.WriteMapNullValue, // 输出空置字段
//			SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null
//			SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null
//			SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null
//			SerializerFeature.WriteNullStringAsEmpty, // 字符类型字段如果为null,输出为"",而不是null
//			SerializerFeature.DisableCircularReferenceDetect //TODO 有了这句就没有$ref了 
//		};
		
		System.out.println( JSONObject.toJSON(map).toString());
		System.out.println( JSONObject.toJSONString(map));
		
		Object o = JSONObject.parseObject(JSONObject.toJSONString(map), Map.class) ;
		Object o2 = JSONObject.parseObject(JSONObject.toJSON(map).toString(), Map.class) ;
		System.out.println(o);
	}

}



看看输出的不同 

解开上边 注释代码,输出结果都没有 $ref

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值