不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson
2024-09-06 04:40:57
net.sf.json 和 fastjson 对于空串和NULL的处理:
1、测试代码
package com.TestMain;
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
public class TestTest {
public static void main(String args[]) throws Exception {
String sDesc = "";
Map<String, String> map = new HashMap<String, String>();
map.put("test", "test");
map.put("str", null);
System.out.println("测试:null");
System.out.println("ALIBAB:"+JSON.toJSONString(map));
System.out.println("net:"+net.sf.json.JSONObject.fromObject(map).toString());
System.out.println("=====");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("test", "test");
map2.put("str", "null");
System.out.println("测试:null字符串");
System.out.println("ALIBAB:"+JSON.toJSONString(map2));
System.out.println("net:"+net.sf.json.JSONObject.fromObject(map2).toString());
System.out.println("=====");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("test", "test");
map3.put("str", "");
System.out.println("测试:空白字符串");
System.out.println("ALIBAB:"+JSON.toJSONString(map3));
System.out.println("net:"+net.sf.json.JSONObject.fromObject(map3).toString());
System.out.println("=====");
}
}
2、测试结果:
测试:null
ALIBAB:{"test":"test"}
net:{"str":null,"test":"test"}
=====
测试:null字符串
ALIBAB:{"str":"null","test":"test"}
net:{"str":null,"test":"test"}
=====
测试:空白字符串
ALIBAB:{"str":"","test":"test"}
net:{"str":"","test":"test"}
=====
3、总结:
- fastJson 根据传入的对象进行序列化,是字符串就是字符串,是NULL就是NULL。序列化结果不包含NULL。
- net.sf.json 会将NULL和NULL字符串都作为NULL处理。序列化结果包含NULL,但是其值也是NULL(不是NULL字符串)。
4、注:Maven中引入net.sf.json的方式
最新文章
- LeetCode Best Time to Buy and Sell Stock with Cooldown
- HTTP请求响应对照表
- [Excel]C#操作Excel(导入导出)
- (转)Mysql 安装与卸载
- hdu 4700 那个啥树
- iOS进阶——App生命周期
- 利用weka和clementine数据挖掘举例
- spring mvc 多视图配置
- 输出第N个素数
- 用JS的for循环打印九九乘法表
- Sublime Text3 Package Control和Emmet插件安装方法
- 面向对象三大特征之封装与static——(Java学习笔记四)
- C#实现Web文件上传的两种方法
- CTR预估中的贝叶斯平滑方法(二)参数估计和代码实现
- MFC学习之CWinApp类
- margin的两个有趣现象:margin合并和margin塌陷
- Java基础学习-HelloWorld案例的编写和运行
- git配置正确且权限已开但是pull或push提示无权限
- CentOS部署PHP环境
- Stylus基本使用
热门文章
- axios并行请求
- 2、Spring Boot 2.x 快速入门
- python自动华 (十八)
- P4317 花神的数论题 动态规划?数位DP
- 2D动画如何做出3D体积感
- 2019.6.24 校内测试 NOIP模拟 Day 2 分析+题解
- 菜单栏伸缩(附jquery-1.12.1.js)
- 一步一步学习FastJson1.2.47远程命令执行漏洞
- WIN7+Qt5.2.0连接oracle数据库的oci驱动的编译
- Flutter移动电商实战 --(18)首页_火爆专区商品接口制作