无法将JsonNull转换为JsonObject

编程入门 行业动态 更新时间:2024-10-27 10:21:18
本文介绍了无法将JsonNull转换为JsonObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从一个JsonObject复制一个primitve属性到另一个jsonObject propertyToBeCopied = source.getAsJsonObject(propertyName); $ 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 。

,它将被转换为 JsonObject

I 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

本文发布于:2023-11-10 02:56:16,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   JsonNull   JsonObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!