GSON JsonObject“不支持的操作异常:空";获取字符串 [英] GSON JsonObject "Unsupported Operation Exception: null" getAsString

查看:31
本文介绍了GSON JsonObject“不支持的操作异常:空";获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行游戏!带有 Scala 的应用程序.我正在执行一个请求,其中响应应该是一个 JSON 字符串.检查调试器时,JsonElement 返回 OK,并按预期返回所有信息.但是,问题是当我尝试在该 JsonElement 上实际运行方法时.

Running a Play! app with Scala. I'm doing a request where the response is expected to be a JSON string. When checking the debugger, the JsonElement returns OK with all information as expected. However, the problem is when I try to actually run methods on that JsonElement.

val json = WS.url("http://maps.googleapis.com/maps/api/geocode/json?callback=?&sensor=true&address=%s", startAddress+","+startCity+","+startProvince).get.getJson
    val geocoder = json.getAsString

我得到的唯一错误是 Unsupported Operation Exception: null 我已经在 getAsStringgetAsJsonObject 上尝试过这个getAsJsonPrimitive

The only error I get back is Unsupported Operation Exception: null and I've tried this on getAsString and getAsJsonObject and getAsJsonPrimitive

知道为什么它在所有方法上都失败了吗?谢谢.

Any idea why it's failing on all methods? Thanks.

推荐答案

也许你的 JsonElement 是一个 JsonNull

你可以做的是首先检查它不是使用 json.isJsonNull

What you could do is to first check that it isn't by using json.isJsonNull

否则,尝试使用 json.toString

这篇关于GSON JsonObject“不支持的操作异常:空";获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆