1

I have a json response which is a list of list of objects, I am trying to serialize it. How do I Serialize it with kotlin serializer?

{
  "body": [
    [
      "Fresho Apple - Royal Gala, Regular, 4 pcs",
      "Red",
      "146",
      82878,
      "Fruit",
      "40033823",
      4,
      600,
      36.5,
      "Turkey",
      "Best before 3 days from delivery date",
      "A combination of slightly tart-tasting skin and honey floral-tasting flesh, the Royal Gala apples, as the name suggests looks regal with beautiful golden-coloured streaks. Royal Gala apples are a good source of fibre and vitamin C, and they are a healthy snack or addition to a meal.",
      620,
      "https://www.bigbasket.com/cookbook/recipes/1611/apple-crumble-pie/",
      "2023-06-06T18:30:00Z",
      false,
      false,
      true
    ]
  ]
}

I have created the data class,

@Serializable
data class Response(
    val body: List<List<Any>>
)
@Serializable
data class Product(
  val title: String,
  val color: String,
  val price: String,
  val id: Int,
  val type: String,
  val ean: String,
  val count: Int,
  val weight: Int,
  val pricePerPiece: Float,
  val countryOfOrigin: String,
  val expiry: String,
  val Description: String,
  val nettWeight: Int,
  val link: String,
  val date: String,
  val isUnavailable: Boolean,
  val isCooked: Boolean,
  val isFruit: Boolean
)

I am stuck with this, how to convert the data type to a list so that we can map it to the Product?

Elo1422
  • 11
  • 1

1 Answers1

-1

It all depends on what your real question is. I couldn't really understand if this is a local JSON or NETWORK. If local: read it -> use this as reference. Use encodeToString/decodeToString. If network you need Retrofit + kotlin Serialization. All well explained here.

Btw. You can't parse it [your JSON] as val body: List<List<Any>>. It should be the obj type you need. i.e. List<Product>. Or List<List<Product>> if there is a nested list.

Good luck!

Samir Ramic
  • 29
  • 1
  • 6