2010-03-16 15 views
7

Ich verwende JSON, um eine Eingabezeichenfolge zu deserialisieren, die einen null-Wert für bestimmte hashmap-Eigenschaft enthält. Hat jemand eine Ahnung, warum diese Ausnahme auftritt? Ist es möglich, dass null nicht als Wert akzeptiert wird Ist das irgendwie konfigurierbar?Java - JSON Null-Ausnahme

Eingangsabtastwert:

{"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) 

Dank.

Antwort

6

Für mich dieses arbeitet mit 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); 

Eigentlich der null Wert eine Instanz von JSONNull wird.

JSONNull entspricht dem Wert, der JavaScript null nennt, während Java null dem Wert entspricht, dass JavaScript undefined nennt.

+0

Danke für Ihre Antwort. – thelost