Vishal Zanzrukia
Vishal Zanzrukia

Reputation: 4973

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

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?

Upvotes: 1

Views: 11142

Answers (3)

Wei Du
Wei Du

Reputation: 1

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

Upvotes: -2

asayamakk
asayamakk

Reputation: 163

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.

Upvotes: 1

Archer
Archer

Reputation: 5147

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

More details here.

Upvotes: 7

Related Questions