https://kotlinlang.org logo
#android
Title
# android
a

Ahren Swett

07/12/2022, 5:49 PM
Help with kotlin Serialization deserializing deep JSON elements. in the element below I am trying to get the sprites: other: official-artwork: but I’m having a really tough time figuring it out I’m not sure how to interact with the dash in the middle of the name I believe the underscore in the front_default will parse nicely but any help appreciated
Copy code
{
  "name": "bulbasaur",
  "id": 1,
  "sprites": {
    "back_default": "String",
    "front_default": "String",
    "front_female": null,
    "other": {
      "dream_world": {
        "front_default": "String",
        "front_female": null
      },
      "official-artwork": {
        "front_default": "String"
      }
    }
  }
}
I have data classes defined as this
Copy code
@OptIn(ExperimentalSerializationApi::class)
val pokemonFormat = Json {
    ignoreUnknownKeys = true
    coerceInputValues = true
    explicitNulls = false
    isLenient = true }

@Serializable
data class Pokemon(
    val name : String?,
    val types : List<Type>,
    val Stats: List<Stat>?,
    val order : Int?,

    //not sure how to use Kotlin Serialization to get this
    val sprites: Map<String, ?????>
)
    
@Serializable
data class Type(
   val type: Map<String,String>
)

@Serializable
data class Stat(
    val stat: Map<String,String>
)

@Serializable
data class Other(
 val other: Map<String,String>
)
t

Tower Guidev2

07/13/2022, 7:54 AM
does this help?
DeepJson.kt,Sprites.kt,DreamWorld.kt,OfficialArtwork.kt,Other.kt,ParsingDeep.kt
5 Views