代码“val jsonObj = json作为JsonObject”在Kotlin中是什么意思?

下面的示例代码是从网页,你能告诉我代码val jsonObj = json as JsonObject意思吗? Kotlin的关键字是单词吗?

谢谢!

 open class WeatherDeserializer : JsonDeserializer<WeatherObject> { override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): WeatherObject? { val jsonObj = json as JsonObject val wheather = WeatherObject() val wind = WindObject() val jsonWeatherArray = jsonObj.getAsJsonArray("weather").get(0) val jsonMainObj = jsonObj.getAsJsonObject("main") val jsonWindObj = jsonObj.getAsJsonObject("wind") wheather.main = jsonWeatherArray.asJsonObject.get("main").asString wheather.description = jsonWeatherArray.asJsonObject.get("description").asString wheather.temp = jsonMainObj.get("temp").asFloat wheather.tempMax = jsonMainObj.get("temp_max").asFloat wheather.tempMin = jsonMainObj.get("temp_min").asFloat wheather.humidity = jsonMainObj.get("humidity").asInt wind.speed = jsonWindObj.get("speed").asFloat wind.deg = jsonWindObj.get("deg").asFloat wheather.wind = wind return wheather } } 

你的json被转换成JsonObjectas Kotlin的一个演员关键字一样。

as我们提供了不安全的表演。 这是一个例外,如果演员不可能抛出。

进一步阅读

as一个类型转换操作符一样。

所以这只是将JsonElementjson对象JsonElementJsonObject

就像Java的JsonObject jsonObject = (JsonObject) json;

有关更多信息,请阅读https://kotlinlang.org/docs/reference/typecasts.html#unsafe-cast-operator