fastjson 如何去掉 $ref,使用方法,不讲原理
2017-02-16 14:46
405 查看
fastjson 是一个 不错的json格式化工具,
但是在使用时,如果 碰到统一地址对象引用,就会用$ref替代 。
怎么去掉ref呢,
这里 有两种方法
1. 使用
如变代码引用 注释,部分
2.使用
多说一点,看下边
String 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
但是在使用时,如果 碰到统一地址对象引用,就会用$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
相关文章推荐
- ps如何去掉图片水印 PS使用仿制图章工具去除水印方法图解
- fastjson使用方法实例
- fastjson 使用方法
- 如何使用fastJson来解析JSON格式数据和生成JSON格式数据
- fast-json.jar的使用方法
- 如何使用web api测试工具siege和ab的post方法来发送json数据
- android fastjson使用方法 fastjson教程
- dede使用方法----如何去掉dede自带的版权
- fastjson 使用方法
- Fastjson简单使用方法
- 使用CSS设置滚动条样式以及如何去掉滚动条的方法
- fast-json.jar的使用方法
- Android网络开发中如何使用JSON进行网络通信---Android_JSON数据通讯方法解析
- fastjson 使用方法
- struts2如何动态调用action的方法以及使用json插件范围json格式的javascript对象
- fastjson 使用方法
- fastjson 使用方法
- fast-json.jar的使用方法
- fastjson.jar使用方法
- FastJSON使用心得——如何解析带中文或者数字key的数据