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 - 海山 - 博客园

不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson的更多相关文章

  1. Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加

    package com.aherp.framework.util; import java.lang.reflect.Array;import java.lang.reflect.Method;imp ...

  2. net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject

    文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...

  3. net.sf.json.JSONException: java.lang.NoSuchMethodException

    在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...

  4. net.sf.json.JSONException: There is a cycle in the hierarchy!

    因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...

  5. net.sf.json

    JSONObject package com.itlwc.test;         import net.sf.json.JSONArray;    import net.sf.json.JSONO ...

  6. 解析json方式之net.sf.json

    前面转载了json解析的技术:fastjson,今天说下另外一种技术. 下载地址 本次使用版本:http://sourceforge.net/projects/json-lib/files/json- ...

  7. net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案

    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...

  8. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  9. 分享一个关于jackson的Json工具类

    直接贴代码: import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.ma ...

随机推荐

  1. linux下core dump--转载

    原文链接:https://www.cnblogs.com/Anker/p/6079580.html   1.前言 一直在从事linux下后台开发,经常与core文件打交道.还记得刚开始从事linux下 ...

  2. 2018 Nowcoder Multi-University Training Contest 5

    Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ ...

  3. win下安装jupyter遇到的问题

    一:安装jupyter 1.首先要用管理员方式打开cmd,没用管理员打开后面安装不上. 2.安装jupyter.我用的是python3,所以用pip3 install jupyter进行安装. 3.安 ...

  4. redis系列(一):安装配置

    1.下载安装 下载可到redis官网下载(可根据自己的操作系统下载,我的是Ubuntu) 目前redis最新版本是5.0.2:下载地址为:http://download.redis.io/releas ...

  5. openstack 本地导入镜像.

    网络很慢,直接本地传. openstack image create "Fedora30" --file Fedora-Cloud-Base-30-1.2.x86_64.qcow2 ...

  6. Geos判断点是否在多边形内

    使用的geo版本是3.5.1 #include <iostream> #include "geos.h" using namespace std; GeometryFa ...

  7. Python学习日记(六)——内置函数和文件操作(lambda)

    lambda表达式 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 == 1: name = 'prime' else: name = 'c ...

  8. 启动maven项目时报错Failed to start component [StandardEngine[Tomcat]]: A child container failed during start

    详细错误信息:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on ...

  9. C++重写(覆盖)、重载、重定义、多态

    1 重写(覆盖)override override是重写(覆盖)了一个方法,以实现不同的功能.一般用于子类在继承父类时,重写(覆盖)父类中的方法.函数特征相同,但是具体实现不同. 重写需要注意: 被重 ...

  10. windows工程总结

    1.win32控制台console程序 运行在MS-DOS环境中的程序.控制台应用程序通常没有可视化的界面,只是通过字符串来显示或者监控程序.控制台程序常常被应用在测试.监控等用途,用户往往只关心数据 ...