-2

Does anyone know how to deserialize a csv containing multiarray list:

Code, values
1234, [[11111, 22222], [44444, 99999]]
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Armin
  • 125
  • 2
  • 6
  • 1
    This is not a valid CSV file. I would be rather surprised if any standard CSV parser could parse it. – Ryan M Mar 01 '22 at 05:29

1 Answers1

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]])
lukas.j
  • 6,453
  • 2
  • 5
  • 24