scala GSON JsonObject“不支持的操作异常:null”getAsString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9324760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GSON JsonObject "Unsupported Operation Exception: null" getAsString
提问by crockpotveggies
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.
运行游戏!带有 Scala 的应用程序。我正在执行一个请求,其中响应应该是一个 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
The only error I get back is Unsupported Operation Exception: nulland I've tried this on getAsStringand getAsJsonObjectand getAsJsonPrimitive
我得到的唯一错误是Unsupported Operation Exception: null,我已经试过了getAsString,getAsJsonObject并且getAsJsonPrimitive
Any idea why it's failing on all methods? Thanks.
知道为什么它在所有方法上都失败了吗?谢谢。
回答by andy petrella
Maybe your JsonElementis a JsonNull
也许你JsonElement是一个JsonNull
What you could do is to first check that it isn't by using json.isJsonNull
你可以做的是首先检查它不是通过使用 json.isJsonNull
Otherwise, try to get its String representation with json.toString
否则,尝试使用 json.toString
回答by lleclerc
I had a similar problem and I had to change jsonObject.getAsString() to jsonObject.toString();
我有一个类似的问题,我不得不将 jsonObject.getAsString() 更改为 jsonObject.toString();
回答by Henry
In my case I just needed to get the element as an empty string if it is null, so I wrote a function like this:
在我的情况下,如果元素为空,我只需要将元素作为空字符串获取,所以我写了一个这样的函数:
private String getNullAsEmptyString(JsonElement jsonElement) {
return jsonElement.isJsonNull() ? "" : jsonElement.getAsString();
}
So instead of
所以代替
val geocoder = json.getAsString
You can just use this
你可以用这个
val geocoder = getNullAsEmptyString(json);
It returns "" if the element is null and the actual string if it is not
如果元素为空,则返回“”,如果不是,则返回实际字符串
回答by Hammond95
The class JsonElementwill throw Unsupported Operation Exceptionfor any getAs<Type>method, because it's an abstract class and makes sense that it is implemented in this way.
该类JsonElement将抛出Unsupported Operation Exception任何getAs<Type>方法,因为它是一个抽象类,并且以这种方式实现它是有意义的。
For some reason the class JsonObject, does not implement the getAs<Type>methods, so any call to one of these methods will throw an exception.
由于某种原因,该类JsonObject没有实现这些getAs<Type>方法,因此对这些方法之一的任何调用都将引发异常。
Calling the toStringmethod on a JsonElementobject, may solve your issue in certain circumstances, but isn't probably what you want because it returns the json representation as String (e.g. \"value\") in some cases.
toString在JsonElement对象上调用该方法可能会在某些情况下解决您的问题,但可能不是您想要的,因为它在某些情况下将 json 表示返回为字符串(例如\"value\")。
I found out that also a JsonPrimitiveclass exists and it does implement the getAs<Type>methods. So probably the correct way to proceed is something like this:
我发现也JsonPrimitive存在一个类,它确实实现了这些getAs<Type>方法。所以可能正确的方法是这样的:
String input = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
JsonParser parser = new JsonParser();
JsonElement jsonTree = parser.parse(input);
if(jsonTree != null && jsonTree.isJsonObject()) {
JsonObject jsonObject = jsonTree.getAsJsonObject();
value = jsonObject.get("key1").getAsJsonPrimitive().getAsString()
}
PS. I removed all the nullability mgmt part. If you are coding in Java you probably want to manage this in a better way.
附注。我删除了所有可空性管理部分。如果您使用 Java 编码,您可能希望以更好的方式管理它。
see GitHub source code for JsonElement:
https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/JsonElement.java#L178
请参阅 GitHub 源代码JsonElement:https:
//github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/JsonElement.java#L178

