fastjson:格式化输出,处理NULL,日期格式化

简介: fastjson:格式化输出,处理NULL,日期格式化

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属性使用

https://blog.csdn.net/fly910905/article/details/78474813


实例

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
}

 


目录
相关文章
|
11月前
|
JSON fastjson 数据格式
利用过滤器简单粗暴的解决FastJson转JSON后字段存在null的问题
如果使用下面fastjson内置的几种策略,通过名字大家基本上知道它们的作用,这种不会过滤掉日期字段的null
300 0
|
JSON fastjson 数据格式
fastjson生成json时Null转为空字符串““或者不展示问题解决
fastjson生成json时Null转为空字符串““或者不展示问题解决
1285 0
|
3月前
|
机器学习/深度学习 SQL 关系型数据库
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
39 0
|
3月前
|
SQL 关系型数据库 MySQL
总结 vue3 的一些知识点:MySQL NULL 值处理
总结 vue3 的一些知识点:MySQL NULL 值处理
|
5月前
|
SQL 关系型数据库 MySQL
MySQL NULL 值处理
MySQL NULL 值处理
|
2月前
|
SQL 关系型数据库 MySQL
python在mysql中插入或者更新null空值
这段代码是Python操作MySQL数据库的示例。它执行SQL查询从表`a_kuakao_school`中选取`id`,`university_id`和`grade`,当`university_id`大于0时按升序排列。然后遍历结果,根据`row[4]`的值决定`grade`是否为`NULL`。若不为空,`grade`被格式化为字符串;否则,设为`NULL`。接着构造UPDATE语句更新`university`表中对应`id`的`grade`值,并提交事务。重要的是,字符串`NULL`不应加引号,否则更新会失败。
27 2
|
5月前
|
存储 关系型数据库 MySQL
Flink CDC中mysql cdc 抽取这个时间字段的值为null 有什么好的解决方案吗 ?
Flink CDC中mysql cdc 抽取这个时间字段的值为null 有什么好的解决方案吗 ?
107 0
|
1月前
|
关系型数据库 MySQL
mysql中判断NULL和空字符串
mysql中判断NULL和空字符串
12 0
|
2月前
|
移动开发 关系型数据库 MySQL
mysql删除为NULL或者空字符串‘‘或者‘null’的或者删除空格的
mysql删除为NULL或者空字符串‘‘或者‘null’的或者删除空格的
15 1