无法将JsonNull转换为JsonObject [英] Can't cast JsonNull to JsonObject
问题描述
我想从一个JsonObject复制一个primitve属性到另一个jsonObject propertyToBeCopied = source.getAsJsonObject(propertyName); $ c
但我总是遇到这种异常情况: 根据文档,应该可以执行转换,或者我错了? 根据 docs 会返回一个 I want to copy one primitve property from one JsonObject to another but I always run into this exception: According to the documentation it should be possible to do the cast, or am I wrong? According to the docs would return a 这篇关于无法将JsonNull转换为JsonObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
com.google .gson.JsonNull不能转换为com.google.gson.JsonObject
JsonNull
是 JsonElement
但不是 JsonObject
(它本身就是一个 JsonElement
)。使用
JsonElement element = source.get(propertyName);
if(!(element instanceof JsonNull)){
JsonObject propertyToBeCopied =(JsonObject)element;
}
JsonElement
如果它不是类型 JsonNull
。
,它将被转换为 JsonObject
JsonObject propertyToBeCopied = source.getAsJsonObject(propertyName);
com.google.gson.JsonNull cannot be cast to com.google.gson.JsonObject
JsonNull
is a JsonElement
but not a JsonObject
(which is itself a JsonElement
). UsingJsonElement element = source.get(propertyName);
if (!(element instanceof JsonNull)) {
JsonObject propertyToBeCopied = (JsonObject) element;
}
JsonElement
that is casted to JsonObject
if it is not of the type JsonNull
.