I'm writing an app in Kotlin. I have a raw JSON string coming from a webservice and I need to use it with Gson.
I'm doing this:
val gson = Gson()
val friends = gson.fromJson(response.rawResponse, JsonElement::class)
but the compiler can't find the correct fromJson
method overload, which is currently available instead (fromJson(json: String!, typeOfT: Type!)
).
That's the error:
Error:(65, 50) None of the following functions can be called with the arguments supplied:
public open fun <T : Any!> fromJson(json: JsonElement!, classOfT: Class<JsonElement!>!): JsonElement! defined in com.google.gson.Gson
public open fun <T : Any!> fromJson(json: JsonElement!, typeOfT: Type!): JsonElement! defined in com.google.gson.Gson
public open fun <T : Any!> fromJson(reader: JsonReader!, typeOfT: Type!): JsonElement! defined in com.google.gson.Gson
public open fun <T : Any!> fromJson(json: Reader!, classOfT: Class<JsonElement!>!): JsonElement! defined in com.google.gson.Gson
public open fun <T : Any!> fromJson(json: Reader!, typeOfT: Type!): JsonElement! defined in com.google.gson.Gson
public open fun <T : Any!> fromJson(json: String!, classOfT: Class<JsonElement!>!): JsonElement! defined in com.google.gson.Gson
public open fun <T : Any!> fromJson(json: String!, typeOfT: Type!): JsonElement! defined in com.google.gson.Gson
What am I doing wrong?
you should pass a java.lang.Class
rather than a kotlin.reflect.KClass
, for example:
val friends = gson.fromJson(response.rawResponse, JsonElement::class.java)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With