Viewed
Viewed

Reputation: 1413

How to skip top key of json?

There is JSON with root key playlist that fetch from Retrofit Interface. Gson library can't parse it to data class. Is there way to do "step into" playlist key before create model?

{
    playlist: {
        name: "",
        description: ""
    }
}
data class Playlist(val name: String, val description: String)

Upvotes: 1

Views: 99

Answers (2)

Mohamed Rejeb
Mohamed Rejeb

Reputation: 2639

You can use that website json to Kotlin, it's very useful specially with long json data. For your case you have two objects, the main object and playlist object, so you need two data classes:

data class PlaylistResponse (
    val playlist: Playlist
)

data class Playlist (
    val name: String,
    val description: String
)

Upvotes: 1

Yakubu
Yakubu

Reputation: 1109

Try this

data class PlaylistObject(
    val playlist: Playlist
)

data class Playlist(
     val description: String,
      val name: String
)

Upvotes: 0

Related Questions