JSONObject与null
前言
今天在写代码的时候发现在 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的更多相关文章
- net.sf.json.JSONObject处理 "null" 字符串的一些坑
转: net.sf.json.JSONObject处理 "null" 字符串的一些坑 2018年05月02日 16:41:25 大白能 阅读数:7026 版权声明:本文为博主原 ...
- Java学习-031-JSON 之五 -- 特定数据获取(JSONObject满足特定键值)
前面几篇博文分别讲述了 JSON 的 概要知识.简单数据获取.封装cssSelector数据获取方法.JSONObject 是否包含 key_value,请自行阅读相关博文. 在日常的接口测试脚本编写 ...
- Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对
前文对获取 JSON 数据封装方法,使之可通过类似于 cssSelector 的方法获取 JSON 数据,使获取数据变得简单.敬请参阅:模仿 cssSelector 封装读取 JSON 数据方法. 在 ...
- org.json.JSONObject与com.google.gson.Gson
org.json库为JSON创始人编写的解析JSON的java库,Gson为Google为我们提供的解析JSON格式数据的库. Gson里最重要的对象有2个Gson 和GsonBuilder. Gso ...
- Android开发将List转化为JsonArray和JsonObject
客户端需要将List<Object>转化为JsonArray和JsonObject的方法: 首先,List中的Object的属性需要是public: class Person { publ ...
- Gson解析JsonObject和JsonArray
Gson中重要的几个核心类: Gson.JsonParser.JsonObject.JsonArray. 下面就是解析的步骤: public void parserJsonArray(String s ...
- jQuery 完成ajax传jsonObject数据,并在后台处理
效果图: 1.js文件封装的几个js工具 <span style="font-family:KaiTi_GB2312;font-size:18px;">//兼容ie i ...
- Java 中判断 JSONObject 对应的 VALUE 为空
目前发现有两种包.两种不一样的json包. 第一种情况是: json包是json-lib包是net.sf.json 怎样判断JSONObject返回的是字符串null还是null值. 研究源码发现.J ...
- java解析复杂json:JSONObject 和 JSONArray的使用
在正式解析之前,我们需要下载解析Json所需要的jar包,一共有7个. 下载地址如下:https://download.csdn.net/download/zai_xia/10374080 大家也可以 ...
随机推荐
- CentOS6.5下telnet服务
00×0 本文介绍Telnet搭建,以及展示这是一个不安全的远程服务. 00×1 服务准备工作 [root@localhost ~]# yum install xinetd telnet-server ...
- docker微服务部署之:五、利用DockerMaven插件自动构建镜像
docker微服务部署之:四.安装docker.docker中安装mysql和jdk1.8.手动构建镜像.部署项目 在上一篇文章中,我们是手动构建镜像,即: 4.1.2.5.1.2.6.1.2中的将d ...
- spring属性配置执行过程,单列和原型区别
Spring配置中,采用属性注入时,当创建IOC容器时,也直接创建对象,并且执行相对应的setter方法 Student.java package com.scope; public class St ...
- Visual Studio各个版本对应关系
- MyBatisSystemException->BindingException: Parameter 'xxx' not found. Available parameters are [arg1, arg0, param1, param2]
最近在使用Spring boot+mybatis做新的基础框架,结果碰到如下问题: 1 org.mybatis.spring.MyBatisSystemException: nested except ...
- slatstack高效运维
一.简介 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. 二.诞生的背景 系统管理员日常会进行大量的重复性操作,例如安装软件, ...
- Mac 10.12通过Launchd创建自定义服务(基于MySQL 5.7.15的开机自启动)
在上一篇文章http://www.cnblogs.com/EasonJim/p/6275863.html中安装MySQL时采用的时DMG包的安装步骤页面进行安装的,如果这样安装的MySQL是会开机自启 ...
- java使用freemarker生成word文档
1.原料 开源jar包freemarker.eclipse.一份模板word文档 2.首先设计模板word文档 一般,通过程序输出的word文档的格式是固定的,例如建立一个表格,将表格的标题写好,表格 ...
- Elastic-Job源码分析之JobScheduler类分析
JobScheduler这个类是EJ中比较核心的一个类,我们现在开始解析这个类. 一.构造器 首先我们看一下JobScheduler的几个构造器. private JobScheduler(final ...
- struts2 ognl存放数据
ongl存放数据可以存放在对象栈(root),也可以存放在map中 一.存放在map中 1.存放在map中可以分为存放在request.session.application public Strin ...