问题
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