I have a JSON string that looks like {"code": "FOO"}
.
Now I want to deserialize this string using kotlinx.serialization
. I've tried the following:
import kotlinx.serialization.*
@Serializable
data class Result(val code: String?)
val decoded = Json.decodeFromString<Result>(jsonString)
This works when the JSON only contains a code
, but in reality there can be other keys inside the JSON string (this is out of my control). I only care about the code
key, but when there are other keys present my app crashes.
How do I only decode the relevant JSON keys?