Reputation: 1275
I want to structure/convert the the following JSON snippet into Kotlin data class such that I can use that in further representation. Sort of expecting list of players and each object to be a player, right now I'm finding it difficult to serialize such cases of same type.
"Players": {
"63084": {
"Position": "1",
"Name_Full": "Imam-ul-Haq",
"Batting": {
"Style": "LHB",
"Average": "60.66",
"Strikerate": "81.54",
"Runs": "910"
},
"Bowling": {
"Style": "LB",
"Average": "0",
"Economyrate": "0",
"Wickets": "0"
}
},
"57492": {
"Position": "2",
"Name_Full": "Fakhar Zaman",
"Batting": {
"Style": "LHB",
"Average": "55.25",
"Strikerate": "96.64",
"Runs": "1326"
},
"Bowling": {
"Style": "SLO",
"Average": "88",
"Economyrate": "4.75",
"Wickets": "1"
}
},
"59429": {
"Position": "3",
"Name_Full": "Babar Azam",
"Batting": {
"Style": "RHB",
"Average": "50.6",
"Strikerate": "84.4",
"Runs": "2328"
},
"Bowling": {
"Style": "OB",
"Average": "0",
"Economyrate": "0",
"Wickets": "0"
}
}
}
Could do something like this:
@SerializedName("63084")
val player: Player
Doesn't even feel efficient repeating for each ids, even the ids would be dynamic and different for each player. Surely missing something important over here.
API: https://cricket.yahoo.net/sifeeds/cricket/live/json/sapk01222019186652.json
Upvotes: 3
Views: 87
Reputation: 316
The API you are consuming is a bit out of standard but you can map Players field to a Map<String, Player> like:
data class Player(
@SerializedName("Position")
val position: String,
@SerializedName("Name_Full")
val name: String
...)
And then:
@SerializedName("Players")
val player: Map<String, Player>
Upvotes: 3