FastJSON

Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. cn.com.bsfit.frms.obj.AuditResult

github: http://git.oschina.net/wenshao/fastjsonhtml

fastjson 使用手册,偷点懒,嘿嘿java

http://www.w3cschool.cn/fastjson/fastjson-quickstart.htmlgit

1.普通github

JSON.toJSONString(对象);
或
JSONObject.toJSONString(对象);

2.自定义输出参数json

   a. 直接在实体中属性中使用注解  bash

@JSONField(serialize=false) //序列化:该字段不输出  可是若是加了final,这个字段就没法被过滤 
@JSONField(derialize=false) //反序列化:该字段不输出  可是若是加了final,这个字段就没法被过滤 
@JSONField(format=”yyyy-MM-dd HH:mm”) //日期按照指定格式输出
@JSONField(name="别名");//使用别名
@JSONField(serialzeFeatures={SerialzeFeatures属性});//序列化
@JSONField(parseFeatures={Features属性});//反序列化

@JSONField(format=”yyyy-MM-dd HH:mm”)ui

   b. 使用toJSONString 的重载方法,添加  SerializerFeature 参数 自定义输入spa

SerializerFeature属性.net

   WriteEnumUsingNamecode

名称 含义 支持版本
QuoteFieldNames 输出key时是否使用双引号,默认为true  
UseSingleQuotes 使用单引号而不是双引号,默认为false  
WriteMapNullValue 是否输出值为null的字段,默认为false  
WriteEnumUsingToString

Enum输出name()或者original,默认为false

用枚举toString()输出

 
WriteEnumUsingToName 用枚举name()输出
UseISO8601DateFormat Date使用ISO8601格式输出,默认为false  
WriteNullListAsEmpty List字段若是为null,输出为[],而非null 1.1
WriteNullStringAsEmpty 字符类型字段若是为null,输出为”“,而非null 1.1
WriteNullNumberAsZero 数值字段若是为null,输出为0,而非null 1.1
WriteNullBooleanAsFalse Boolean字段若是为null,输出为false,而非null 1.1
SkipTransientField

若是是true,类中的Get方法对应的Field是transient

,序列化时将会被忽略。默认为true

1.1
SortField 按字段名称排序后输出。默认为false 1.1
WriteTabAsSpecial 把\t作转义输出,默认为false 1.1.1 (不推荐)
PrettyFormat 结果是否格式化,默认为false 1.1.2
WriteClassName 序列化时写入类型信息,默认为false。反序列化是需用到 1.1.2
DisableCircularReferenceDetect 消除对同一对象循环引用的问题,默认为false 1.1.6
WriteSlashAsSpecial 对斜杠’/’进行转义 1.1.9
BrowserCompatible

将中文都会序列化为\uXXXX格式,字节数会多一些

,可是能兼容IE 6,默认为false

1.1.10
WriteDateUseDateFormat

全局修改日期格式,默认为false。

1.1.14
NotWriteRootClassName   1.1.15
DisableCheckSpecialChar

一个对象的字符串属性中若是有特殊字符如双引号,

将会在转成json时带有反斜杠转移符。若是不须要转义,

能够使用这个属性。默认为false

1.1.19
BeanToArray 将对象转为array输出 1.1.35
WriteNonStringKeyAsString

Map<Integer,Stirng> 输出 key 默认为 Integer

添加此属性,Integer 添加了 “”,变成字符型

1.1.37
NotWriteDefaultValue 将基础类型的默认值屏蔽 1.1.42
BrowserSecure   1.2.6
IgnoreNonFieldGetter   1.2.7

修改 fastjson 日期默认日期方式。

JSON.DEFFAULT_DATE_FORMAT = "YYYY-MM-dd HH:mm:ss";
JSON.toJSONString(topic,SerializerFeature.WriteDateUseDateFormat);

或者

JSONObject.toJSONStringWithDateFormat(topic,"YYYY-MM-dd HH:mm:ss")

报错:

Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. cn.com.bsfit.frms.obj.AuditResult

修改方式:

ParserConfig.getGlobalInstance().setAutoTypeSupport(true);

输出保持输入顺序:

JSONObject ao = new JSONObject(true);//默认使用 LinkedHashMap
JSON.toJSONStringZ(ao,SerializeConfig.getGlobalInstance(),SerializerFeature.WriteMapNullValue,SerializerFeature.QuoteFieldNames);

 

注意:SerializerFeature.QuoteFieldNames 默认为true ,方法添加不起做用,缘由不明,彷佛没法修改默认值true 为 false。

相关文章
相关标签/搜索