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的方式

  1. maven 无法引入 net.sf.json 的解决方法 - vili_sky 的博客 - CSDN 博客
  2. json-lib 的 maven dependency - 海山 - 博客园

最新文章

  1. LeetCode Best Time to Buy and Sell Stock with Cooldown
  2. HTTP请求响应对照表
  3. [Excel]C#操作Excel(导入导出)
  4. (转)Mysql 安装与卸载
  5. hdu 4700 那个啥树
  6. iOS进阶——App生命周期
  7. 利用weka和clementine数据挖掘举例
  8. spring mvc 多视图配置
  9. 输出第N个素数
  10. 用JS的for循环打印九九乘法表
  11. Sublime Text3 Package Control和Emmet插件安装方法
  12. 面向对象三大特征之封装与static——(Java学习笔记四)
  13. C#实现Web文件上传的两种方法
  14. CTR预估中的贝叶斯平滑方法(二)参数估计和代码实现
  15. MFC学习之CWinApp类
  16. margin的两个有趣现象:margin合并和margin塌陷
  17. Java基础学习-HelloWorld案例的编写和运行
  18. git配置正确且权限已开但是pull或push提示无权限
  19. CentOS部署PHP环境
  20. Stylus基本使用

热门文章

  1. axios并行请求
  2. 2、Spring Boot 2.x 快速入门
  3. python自动华 (十八)
  4. P4317 花神的数论题 动态规划?数位DP
  5. 2D动画如何做出3D体积感
  6. 2019.6.24 校内测试 NOIP模拟 Day 2 分析+题解
  7. 菜单栏伸缩(附jquery-1.12.1.js)
  8. 一步一步学习FastJson1.2.47远程命令执行漏洞
  9. WIN7+Qt5.2.0连接oracle数据库的oci驱动的编译
  10. Flutter移动电商实战 --(18)首页_火爆专区商品接口制作