GSON JsonObject“不支持的操作异常:null”符getAsString [英] GSON JsonObject "Unsupported Operation Exception: null" getAsString
问题描述
运行一个游戏!应用程序与斯卡拉。我正在做一个请求,其中的响应预计是一个JSON字符串。在检查调试器时,JsonElement会返回OK,包含所有预期的信息。但是,问题是当我尝试在该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操作异常:null
我在 getAsString
和 getAsJsonObject
和 getAsJsonPrimitive
任何想法为什么它在所有方法上失败?感谢。
可能是您的 JsonElement
是 JsonNull
你可以做的是首先检查它是否使用 json.isJsonNull
否则,请尝试使用 json.toString
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
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.
Maybe be your JsonElement
is a JsonNull
What you could do is to first check that it isn't by using json.isJsonNull
Otherwise, try to get its String representation with json.toString
这篇关于GSON JsonObject“不支持的操作异常:null”符getAsString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!