What does the sentinel value NULL mean in Android Developer JSONObject reference page

StackOverflow https://stackoverflow.com/questions/18199282

  •  24-06-2022
  •  | 
  •  

سؤال

What does the sentinel value NULL mean in Android Developer JSONObject reference page, cited here for convenience:

Warning: this class represents null in two incompatible ways: the standard Java null reference, and the sentinel value NULL. In particular, calling put(name, null) removes the named entry from the object but put(name, JSONObject.NULL) stores an entry whose value is JSONObject.NULL.


UPDATE

All that I can find online is this.

هل كانت مفيدة؟

المحلول

This means that the Standard Java null values in Json Object causes them to be ignored and do-not appear in the output. This is baiscally done to compact the output. Where as the sentinel JSONObject.NULL will add null in the output string.

An example will make you clear

JSONObject jObject = new JSONObject();
jObject.put("First Name", null);
jObject.put("Last Name", JSONObject.NULL);
//Print jObject
Log.d("JSON OBJECT", jObject.toString());

This will give the output like

{"Last Name":null}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top