不用管我下面的第一個答案。我讀得太快了。node
看起來這是一個簡單的例子,文件撒謊-或者至少是被誤解了。幸運的是,代碼並非那麼簡單,並且gson是一個開源項目。ui
這是 JsonObject.get(String)
:this
/** * Returns the member with the specified name. * * @param memberName name of the member that is being requested. * @return the member matching the name. Null if no such member exists. */ public JsonElement get(String memberName) { if (members.containsKey(memberName)) { JsonElement member = members.get(memberName); return member == null ? JsonNull.INSTANCE : member; } return null; }
這裏是 members
人口:spa
/** * Adds a member, which is a name-value pair, to self. The name must be a String, but the value * can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements * rooted at this node. * * @param property name of the member. * @param value the member object. */ public void add(String property, JsonElement value) { if (value == null) { value = JsonNull.INSTANCE; } members.put($Gson$Preconditions.checkNotNull(property), value); }
添加到 members
是爲Java類中定義的每一個成員建立的-它不是基於JSON中的內容。(對於有興趣的人來講,visitFieldsReflectively
方法ReflectingFieldNavigator
(人口成員。)code
所以,我想混淆了「若是沒有議員」這一句中「成員」一詞的含義。基於代碼,我認爲JavaDoc的做者指的是Java類中定義的一個成員。對於gsonAPI的臨時用戶-就像我本身同樣-我假設「成員」指的是JSON中的一個對象元素。對象
如今,這個問題清楚了嗎?blog
基於快速閱讀問題的第一個答案(爲有用的連接保留):ci
A null
引用不是 JsonNull
價值。(value == null)
與value.isJsonNull()
。他們很不同。文檔
文檔描述了調用 JsonObject.get(String)
若是不存在這樣的成員,則返回「[null]」。他們沒有這麼說 JsonNull
會被歸還。get
打電話給 JsonElement.isJsonNull()
不是檢查 JsonElement
引用是null
推薦信。事實上,若是是null
引用時,對其調用方法將引起NullPointerException
。它在檢查它是不是JsonNull
舉個例子。