Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - JSON Null Exception

I'm using JSON to deserialize an input string that contains a null value for certain hashmap property. Does anyone have any clue why this exception occurs ? Is it possible that null is not accepted as a value Is this configurable somehow ?

input sample:

{"prop1":"val1", "prop2":123, "prop3":null}

stacktrace:

net.sf.json.JSONException: null object
    at net.sf.json.JSONObject.verifyIsNull(JSONObject.java:2856)
    at net.sf.json.JSONObject.isEmpty(JSONObject.java:2212)

Thanks.

like image 613
user1112111 Avatar asked Mar 16 '10 16:03

user1112111


People also ask

Is null allowed in JSON?

JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

Is empty object valid JSON?

In JSON Schema, an empty object is a completely valid schema that will accept any valid JSON. You can also use true in place of the empty object to represent a schema that matches anything, or false for a schema that matches nothing.


1 Answers

For me this works with json-lib-2.3-jdk15:

String json = "{\"prop1\":\"val1\", \"prop2\":123, \"prop3\":null}";
JSONObject jsonObject = JSONObject.fromObject(json);
HashMap<String, Object> map = (HashMap<String, Object>) JSONObject.toBean(jsonObject, HashMap.class);

Actually the null value becomes an instance of JSONNull.

JSONNull is equivalent to the value that JavaScript calls null, whilst Java's null is equivalent to the value that JavaScript calls undefined.

like image 99
hleinone Avatar answered Sep 20 '22 04:09

hleinone