前言

今天在写代码的时候发现在 JSON 中 由于put了key对应的value为null,结果这个JSON键值对没有输出

org.json.JSONObject

在orgJSON 中,如果直接put value会有报错提示:

org.json.JSONObject orgJSON = new org.json.JSONObject();
// IDE提示The method put(String, Collection) is ambiguous for the type JSONObject
orgJSON.put("null", null);

但是,org.json.JSONObject 提供了一个JOSNObject.NULL

orgJSON.put("JSONObject.NULL", org.json.JSONObject.NULL);
System.out.println("JSONObject.NULL : "+orgJSON); // 控制台输出
JSONObject.NULL : {"JSONObject.NULL":null}
System.out.println(JSONObject.NULL == null);
// 控制台
false

通过查阅org.json.JSONObject的文档,发现文档是这样说的

The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object.

参考文档:https://stleary.github.io/JSON-java/org/json/JSONObject.html

net.sf.json.JSONObjcet

net.sf.json.JSONObject netJSON = new net.sf.json.JSONObject();
netJSON.put("null", null);
System.out.println("netJSON" + netJSON);
// 控制台输出 :
netJSON{}

文档中这样写的:

The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONNull object.

但是吧

// IDE提示:net.sf.json.JSONNull cannot be resolved to a variable
netJSON.put("JSONNull", net.sf.json.JSONNull);

参考文档:http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/JSONObject.html

JSONObject与null的更多相关文章

  1. net.sf.json.JSONObject处理 "null" 字符串的一些坑

    转: net.sf.json.JSONObject处理 "null" 字符串的一些坑 2018年05月02日 16:41:25 大白能 阅读数:7026   版权声明:本文为博主原 ...

  2. Java学习-031-JSON 之五 -- 特定数据获取(JSONObject满足特定键值)

    前面几篇博文分别讲述了 JSON 的 概要知识.简单数据获取.封装cssSelector数据获取方法.JSONObject 是否包含 key_value,请自行阅读相关博文. 在日常的接口测试脚本编写 ...

  3. Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对

    前文对获取 JSON 数据封装方法,使之可通过类似于 cssSelector 的方法获取 JSON 数据,使获取数据变得简单.敬请参阅:模仿 cssSelector 封装读取 JSON 数据方法. 在 ...

  4. org.json.JSONObject与com.google.gson.Gson

    org.json库为JSON创始人编写的解析JSON的java库,Gson为Google为我们提供的解析JSON格式数据的库. Gson里最重要的对象有2个Gson 和GsonBuilder. Gso ...

  5. Android开发将List转化为JsonArray和JsonObject

    客户端需要将List<Object>转化为JsonArray和JsonObject的方法: 首先,List中的Object的属性需要是public: class Person { publ ...

  6. Gson解析JsonObject和JsonArray

    Gson中重要的几个核心类: Gson.JsonParser.JsonObject.JsonArray. 下面就是解析的步骤: public void parserJsonArray(String s ...

  7. jQuery 完成ajax传jsonObject数据,并在后台处理

    效果图: 1.js文件封装的几个js工具 <span style="font-family:KaiTi_GB2312;font-size:18px;">//兼容ie i ...

  8. Java 中判断 JSONObject 对应的 VALUE 为空

    目前发现有两种包.两种不一样的json包. 第一种情况是: json包是json-lib包是net.sf.json 怎样判断JSONObject返回的是字符串null还是null值. 研究源码发现.J ...

  9. java解析复杂json:JSONObject 和 JSONArray的使用

    在正式解析之前,我们需要下载解析Json所需要的jar包,一共有7个. 下载地址如下:https://download.csdn.net/download/zai_xia/10374080 大家也可以 ...

随机推荐

  1. Linux安装yum的痛苦路程(失败,慎入)

    1,在网上下载了一个yum 的 rpm文件(yum-3.2.29-81.el6.centos.noarch.rpm),我在 http://www.rpmfind.net/linux/rpm2html/ ...

  2. Writing analyzers

    Writing analyzers There are times when you would like to analyze text in a bespoke fashion, either b ...

  3. leetcode-695-Max Area of Island(BFS)

    题目描述: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...

  4. 线性表中顺序表的的理解和实现(java)

    线性表的顺序表示指的是用一组地址连续的存储单元以此存储线性表的数据元素,这种表示也称作线性表的顺序存储结构或顺序映像.通常,称这种存储结构的线性表为顺序表.特点是:逻辑上相邻的数据元素,其物理次序上也 ...

  5. H01-Linux系统中搭建Hadoop和Spark集群

    前言 1.操作系统:Centos7 2.安装时使用的是root用户.也可以用其他非root用户,非root的话要注意操作时的权限问题. 3.安装的Hadoop版本是2.6.5,Spark版本是2.2. ...

  6. BurpSuite系列----Extender模块(扩展器)

    一.简介 Burp在软件中提供了支持第三方拓展插件的功能,方便使用者编写自己的自定义插件或从插件商店中安装拓展插件.Burp扩展程序可以以多种方式支持自定义Burp的行为,例如:修改HTTP请求和响应 ...

  7. 本地搭建sass运行环境

    1.安装node.js 安装文件为msi文件,可到node.js官网下载安装包,下载路径为:https://nodejs.org/en/download/ 安装路径为默认路径,安装完成之后配置环境变量 ...

  8. Ubuntu18.04配制阿里巴巴的源

    配制阿里巴巴的源步骤 使用阿里巴巴的开源镜像:https://opsx.alibaba.com/mirror 然后选择ubuntu的帮助选项,复制ubuntu18.04镜像源 设置root账户密码: ...

  9. PHP常见缓存机制

    PHP常用的几种缓存机制_PHP教程 普遍缓存技术 数据缓存:这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据, ...

  10. [转] Linux History(历史)命令用法 15 例

    [From]https://linuxtoy.org/archives/history-command-usage-examples.html 如果你经常使用 Linux 命令行,那么使用 histo ...