我想从一个JsonObject复制一个primitve属性到另一个jsonObject propertyToBeCopied = source.getAsJsonObject(propertyName); $ c
$ b
但我总是遇到这种异常情况:
com.google .gson.JsonNull不能转换为com.google.gson.JsonObject
根据文档,应该可以执行转换,或者我错了?
根据 docs JsonNull 是 JsonElement 但不是 JsonObject (它本身就是一个 JsonElement )。使用
JsonElement element = source.get(propertyName); if(!(element instanceof JsonNull)){ JsonObject propertyToBeCopied =(JsonObject)element; }会返回一个 JsonElement 如果它不是类型 JsonNull 。
,它将被转换为 JsonObjectI want to copy one primitve property from one JsonObject to another
JsonObject propertyToBeCopied = source.getAsJsonObject(propertyName);
but I always run into this exception:
com.google.gson.JsonNull cannot be cast to com.google.gson.JsonObject
According to the documentation it should be possible to do the cast, or am I wrong?
解决方案According to the docsJsonNull is a JsonElement but not a JsonObject (which is itself a JsonElement). Using
JsonElement element = source.get(propertyName); if (!(element instanceof JsonNull)) { JsonObject propertyToBeCopied = (JsonObject) element; }would return a JsonElement that is casted to JsonObject if it is not of the type JsonNull.
更多推荐
无法将JsonNull转换为JsonObject
发布评论