fastjson解析null值问题: 解决 null的属性不显示问题
2023-04-18 15:01:49 时间
fastjson解析null值问题: 解决 null的属性不显示问题
null对应的key被过滤掉;这明显不是我们想要的结果,这时我们就需要用到fastjson的SerializerFeature序列化属性:
也就是这个方法:
JSONObject.toJSONString(Object object, SerializerFeature... features)
SerializerFeature有用的一些枚举值
QuoteFieldNames———-输出key时是否使用双引号,默认为true
WriteMapNullValue——–是否输出值为null的字段,默认为false
WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
现在加上
Map < String , Object > jsonMap = new HashMap< String , Object>();
jsonMap.put("a",1);
jsonMap.put("b","");
jsonMap.put("c",null);
jsonMap.put("d","wuzhuti.cn");
String str = JSONObject.toJSONString(jsonMap,SerializerFeature.WriteMapNullValue);
System.out.println(str);
//输出结果:{"a":1,"b":"","c":null,"d":"wuzhuti.cn"}
一个代码实例:
private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, Object>>() {
};
/**
* @param tenantId
* @param sql
* @param timeout
* @return
* @throws SQLException
*/
@Override
public List<Map<String, Object>> queryWithJsonFormat(Long tenantId, String sql, long timeout) throws SQLException {
// 获取驱动信息
Map<Long, ConnectInfo> connectInfoMap = getDriverInfo().getConnectInfoMap();
String clusterName = connectInfoMap.get(tenantId).getClusterName();
String response = HttpClientUtils.postTextRequest(baseURL(clusterName), buildSQLWithTimeoutAndFormatJson(sql, timeout));
if (response == null || response.isEmpty()) {
return Collections.emptyList();
}
JSONArray dataArray = (JSONArray) JSON.parseObject(response).get("data");
return dataArray.stream()
.map(it ->
JSON.parseObject(JSON.toJSONString(it, SerializerFeature.WriteMapNullValue), MAP_TYPE_REFERENCE)
)
.collect(Collectors.toList());
}
其中, response 返回值的数据结构:
{
"meta": [
{
"name": "uid_basic_profile_edu_degree",
"type": "Nullable(String)"
},
{
"name": "count_uid_basic_profile_edu_degree",
"type": "UInt64"
}
],
"data": [
{
"uid_basic_profile_edu_degree": "college",
"count_uid_basic_profile_edu_degree": 533389
},
{
"uid_basic_profile_edu_degree": null,
"count_uid_basic_profile_edu_degree": 0
},
{
"uid_basic_profile_edu_degree": "undergraduate",
"count_uid_basic_profile_edu_degree": 2518436
},
{
"uid_basic_profile_edu_degree": "master",
"count_uid_basic_profile_edu_degree": 271653
},
{
"uid_basic_profile_edu_degree": "high",
"count_uid_basic_profile_edu_degree": 5585609
}
],
"rows": 5,
"statistics": {
"elapsed": 0.703029803,
"rows_read": 1287687385,
"bytes_read": 76340471397
}
}
相关文章
- scratch最大公约数 电子学会图形化编程scratch等级考试四级真题和答案解析2022-12
- 再谈解决“因为文件包含病毒或潜在的垃圾软件”系统找不到gpedit.msc
- 2022IDEA的下载、安装、配置与使用
- 瑞吉外卖项目剩余功能补充
- Mac下安装VScode和配置C++环境
- 入门力扣自学笔记247 C++ (题目编号:1605)
- RabbitMQ详解,用心看完这一篇就够了【重点】
- 第10篇:Mac/Windows安装ElasticSearch可视化Head插件
- Rabbitmq面试题总结,非常详细,杜绝标题党,不详细你打我,下次不写博客了
- React中events的踩坑指南
- biopython 报错【no such file:mkdssp】
- 【操作系统复习】信号量机制 & 经典同步问题
- 【2022新版】Java 终极学习路线(文末高清大图)-共计9大模块/6大框架/13个中间件
- Mac 修改Hosts文件的方式
- fasync实现通过文件修改代码中的变量值大小
- 云原生中间件RocketMQ-消费者核心参数、消费模式之集群模式
- 通俗易懂的讲讲什么是中间件?
- 云计算的三种服务模式:IaaS、PaaS和SaaS
- WebLogic Server 远程代码执行漏洞复现 (CVE-2023-21839)
- 【中间件】MQ是什么?RabbitMQ又是什么?