0

我有以下 JSON 数组:

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

我想将它转换为一个对象,但问题是我有嵌套数组,下面的解决方案不起作用;

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
)
4

1 回答 1

0

你需要改变你的json

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

        [
      {
            "body": "Text",
            "bodyType": "Text",
            "nameType": "Text",
            "makeDisplay": "Acura"
        },
        {
            "body": "text",
            "bodyType": "text",
            "nameType": "text",
            "makeDisplay": "text"
        }
    ]
于 2019-08-20T23:00:19.047 回答