Java 无法在 JSON 对象中放置空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44631604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:38:17  来源:igfitidea点击:

Unable to put null values in JSON object

javaandroidjson

提问by Aishwarya

I am trying to pass parameter to api using JSON.

我正在尝试使用 JSON 将参数传递给 api。

class Sample
{ ...
   String token;
...

void method()
{ ...
    JSONObject params = new JSONObject();
    params.put(KEY_TOKEN,token);
    params.put(KEY_DATE,date);

    Log.e("params ",params+"");


      ...  }    

I get the value of params as {"date":"2017-06-19"}but token is seen nowhere. I have not initialized token and its value is null as its an instance variable. So is it something that uninitialized value are not included?

我得到了 params 的值,{"date":"2017-06-19"}但是在任何地方都看不到令牌。我还没有初始化令牌,它的值为 null 作为它的一个实例变量。那么是否不包括未初始化的值?

采纳答案by T.J. Crowder

Right there in the documentation, in the first paragraph:

就在文档中,在第一段中:

Values may not be null, NaNs, infinities, or of any type not listed here.

值可能不是nullNaNs、无穷大或此处未列出的任何类型。

So yes, it is "...something that nullvalues are not included..."(edit: that was a quote from your original question; your updated question changes it to "uninitialized values" but the default value of an object reference is null, so...)

所以是的,它是“......null不包括值的东西......” (编辑:这是你原始问题的引用;你更新的问题将其更改为“未初始化的值”,但对象引用的默认值是null, 所以...)

It's a "feature" of that class, though; JSONitself understands nulljust fine. Further down in the documentation it says you use a "sentinal value," NULL, to represent null. Which seems...odd. There's a note about it:

不过,这是该课程的“特征”;JSON本身null就很好理解。在文档的进一步下方,它说您使用“信号值”NULL来表示null. 这似乎……很奇怪。有一个关于它的注释:

Warning: this class represents nullin two incompatible ways: the standard Java nullreference, and the sentinel value NULL. In particular, calling put(name, null)removes the named entry from the object but put(name, JSONObject.NULL)stores an entry whose value is JSONObject.NULL.

警告:此类null以两种不兼容的方式表示:标准 Javanull引用和标记值NULL。特别是,调用put(name, null)会从对象中删除命名条目,但会put(name, JSONObject.NULL)存储值为 的条目JSONObject.NULL

So:

所以:

params.put(KEY_TOKEN, token == null ? JSONObject.NULL : token);

回答by Priya Jain

Json itself accpets null but JSONOBJECT class does not.Hence, you are not able to do that. Also Try using Hymanson/gson for json and object mapping instead. If you need help with it, let me know.

Json 本身接受 null 但 JSONOBJECT 类不接受。因此,您不能这样做。也可以尝试使用 Hymanson/gson 来代替 json 和对象映射。如果您需要帮助,请告诉我。

回答by Stephen C

According to RFC 4627, JSON treats the nullsymbol as a valid value.

根据RFC 4627,JSON 将null符号视为有效值。

The catch is that JSON nullrepresents the Javascript nullvalue. By contrast, the Java version of nullis (according to the experts) more closely aligned with Javascript's undefinedvalue.

问题在于 JSONnull表示 Javascriptnull值。相比之下,Java 版本null(根据专家的说法)更接近于 Javascript 的undefined价值。

The original author of the org.jsonlibrary decided that JSONObjectshould treat JSON nullin a way that is consistent with the Javascript semantics. Hence it is represented (in Java) as JSONObject.NULL.

org.json库的原作者决定JSONObject应该null以与 Javascript 语义一致的方式处理 JSON 。因此,它表示(在 Java 中)为JSONObject.NULL.

回答by Lars

I solved this problem by creating the null JSON value using a string representation to stuff the null value into the object. Clearly a hack, but it works.

我通过使用字符串表示创建空 JSON 值来将空值填充到对象中来解决这个问题。显然是一个黑客,但它的工作原理。

JSONObject obj = new JSONObject("{\"keyword\": null}");
System.out.println(obj); // {"keyword": null}