Java:JSONObject isNull方法无法正常工作

问题描述

我需要你的帮助。我有像JSONObject

 JSONObject base = {"json" : {"key1" : "somethings","key2" : null}}

我已经写过这样的代码

JSONObject json = base.getJSONObject("json");

json.isNull("key2") // false

json.get("key2").getClass() // class.org.json.JSONObject$Null
json.opt("key2").getClass() // class.org.json.JSONObject$Null
JSONObject.NULL.getClass() // class.org.json.JSONObject$Null
json.get("key2").equals(JSONObject.NULL) // false

仅适用于:

 json.get("key2").equals(null) // true

在isNull()正常工作之前,我更改了JSON中的某些逻辑结构时,问题就浮现了。可能发生或改变了什么?

解决方法

您是否尝试过通过构造函数实例化JSON对象?

JSONObject base = new JSONObject ("{\"json\" : {\"key1\" : \"somethings\",\"key2\" : null}}")

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...