-2

有谁知道如何反序列化包含多数组列表的 csv:

Code, values
1234, [[11111, 22222], [44444, 99999]]
4

1 回答 1

0
val csv = """
Code, Values
1234, [[11111, 22222], [44444, 99999]]
1235, [[11], [21, 22], [31, 32, 33], [41, 42, 43, 44]]
"""

data class Item(
  val Code: Int,
  val Values: List<List<Int>>
)

val result: List<Item> = csv
  .trimIndent()
  .lines()
  .drop(1)
  .map { line ->
    Item(
      line
        .substringBefore(",")
        .toInt(),
      line
        .substringAfter(",")
        .replace(" ", "")
        .removeSurrounding("[[", "]]")
        .split("],[")
        .map { group ->
          group
            .split(",")
            .map { value ->
              value.toInt()
            }
        }

    )
  }

result.forEach { println(it) }

// Output:
// Item(Code=1234, Values=[[11111, 22222], [44444, 99999]])
// Item(Code=1235, Values=[[11], [21, 22], [31, 32, 33], [41, 42, 43, 44]])
于 2022-03-02T17:48:24.073 回答