1

I have JSON string as below

var purchases = "[{\"contentId\":\"861723b2-78e4-42ea-8479-40fa0850c314\",\"purchaseType\":\"RENT\"},{\"contentId\":\"138bc762-b539-4749-8706-de2f0eea4b61\",\"purchaseType\":\"PURCHASE\"}]"

I am trying to convert it into list of data class objects

val userPurchases: List<UserPurchases> = Gson().fromJson(purchases,List<UserPurchases>)

This is my data class

data class UserPurchases(
    val contentId: String,
    val purchaseType: String)
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • 3
    https://stackoverflow.com/questions/33381384/how-to-use-typetoken-generics-with-gson-in-kotlin – IR42 Aug 13 '20 at 14:21

1 Answers1

3

Solved by below

val purchase: List<UserPurchases> = GsonBuilder().create().fromJson(userPurchases, Array<UserPurchases>::class.java).toList()
WISHY
  • 11,067
  • 25
  • 105
  • 197