How can I put key with null value in net.sf.json.JSONObject?

♀尐吖头ヾ 提交于 2019-12-11 10:20:33

问题


I am using net.sf.json.JSONObject. It will remove key if my value is null while I am trying to put data in Json object using put method.

Is there any way to keep my key as it is in my object even my value is null?

for ex.

 JSONObject json = new JSONObject();
 json.put("myKey",null);

Then my output is : {}

But I want

output : {"myKey",""}

How can I do?


回答1:


json.put("myKeyu",JSONObject.NULL);

More details here.




回答2:


JSONObject.NULL is for Apache Commons JSONObject.

Try the following instead:

json.put("key", JSONNull.getInstance())

Here follows the documentation for net.sf.json.JSONObject:
http://json-lib.sourceforge.net/apidocs/index.html

Search for JSONNull to find out more.




回答3:


JSONObject backJsonObject = new JSONObject();
        backJsonObject.put("111", "NULL");
        backJsonObject.put("111", "null");  how to output {"111":"null"} instead of  {"111":null}  in net.sf.json


来源:https://stackoverflow.com/questions/18932188/how-can-i-put-key-with-null-value-in-net-sf-json-jsonobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!