0

I have the following JSON array:

[
  [
    {
      "body": "Text",
      "bodyType": "Text",
      "nameType": "Text",
      "makeDisplay": "Acura"
    }
  ],
  [
    {
      "body": "text",
      "bodyType": "text",
      "nameType": "text",
      "makeDisplay": "text"
    }
  ]
]

I want to convert it to an object, but the problem is that I have nested arrays and the solution below doesn't work;

private var items: List<CarModel> = emptyList()

items = Json(JsonConfiguration.Stable).parse(CarsResponse.serializer().list, "MiJSON.json ....")

@Serializable
data class CarsResponse(
    val items: List<ItemsModels> = emptyList()
)

@Serializable
data class ItemsModels(
    val items: List<CarModel> = emptyList()
)

@Serializable
data class CarModel(
    val body: String = EMPTY_STRING,
    val bodyType: String = EMPTY_STRING,
    val nameType: String = EMPTY_STRING,
    val makeDisplay: String = EMPTY_STRING
)
user2340612
  • 10,053
  • 4
  • 41
  • 66
Gilberto Ibarra
  • 395
  • 1
  • 3
  • 8

1 Answers1

0

You need to change your json from

[
 [
{
  "body": "Text",
  "bodyType": "Text",
  "nameType": "Text",
  "makeDisplay": "Acura"
}
],
[
{
  "body": "text",
  "bodyType": "text",
  "nameType": "text",
  "makeDisplay": "text"
 }
 ]
 ]

to

        [
      {
            "body": "Text",
            "bodyType": "Text",
            "nameType": "Text",
            "makeDisplay": "Acura"
        },
        {
            "body": "text",
            "bodyType": "text",
            "nameType": "text",
            "makeDisplay": "text"
        }
    ]
David Innocent
  • 606
  • 5
  • 16