fastjson:格式化输出,处理NULL,日期格式化
2023-09-11 14:15:39 时间
JSON.toJSONString 源码
/**
* This method serializes the specified object into its equivalent Json representation. Note that this method works fine if the any of the object fields are of generic type,
* just the object itself should not be of a generic type. If you want to write out the object to a
* {@link Writer}, use {@link #writeJSONString(Writer, Object, SerializerFeature[])} instead.
*
* @param object the object for which json representation is to be created setting for fastjson
* @return Json representation of {@code object}.
*/
public static String toJSONString(Object object) {
return toJSONString(object, emptyFilters);
}
public static String toJSONString(Object object, SerializerFeature... features) {
return toJSONString(object, DEFAULT_GENERATE_FEATURE, features);
}
从源码,我们可以知道:fastjson中object转string时的配置项,包括
1. 是否显示value为null的项
2. 是否格式化显示字符串
3. 日期是否格式化显示为可读字符串
fastjson:SerializerFeature属性使用
实例
public static void main(String[] args) {
// 输出结果
JSONObject param = new JSONObject();
param.put("sign","123");
param.put("reqMsg",null);
param.put("creatTime",new Date());
String jsonString = JSON.toJSONString(param, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
System.out.println("【输出结果】\r\n"+jsonString);
}
输出结果:
【输出结果】
{
"creatTime":"2019-06-18 11:03:14",
"sign":"123",
"reqMsg":null
}
相关文章
- SpringMVC配置全局日期转换器,处理日期转换异常
- mysql中的日期转换函数(类似oracle中的to_date)
- 格式化日期的输出
- linux中用shell获取昨天、明天或多天前的日期
- MFC Windows 程序设计[198]之时间日期控件(附源码)
- Oracle 数据库中日期时间的插入操作
- SAP UI5 日期类型 sap.ui.model.type.Date 的显示格式问题分析
- 获取数据库服务器日期、时间
- Atitit.基于时间戳的农历日历历法日期计算
- Atitit.基于时间戳的农历日历历法日期计算
- Atitit.国际化中的日期与钱符号问题
- SAP Fiori应用里日期格式的显示奥秘
- ZZNUOJ_用C语言编写程序实现1211:日期排序(附完整源码)
- C程序-蓝桥-日期计算
- 2020年第十一届蓝桥杯省赛B组真题:回文日期
- 指定 年 周 星期 获取这周的开始日期和结束日期