1

我从以下工作代码开始......

val source = scala.io.Source.fromFile(path)
val after = try source.mkString finally source.close()
val mapper: CsvMapper = new CsvMapper()
val it: MappingIterator[util.List[String]] =
  mapper.readerForListOf(classOf[String])
    .withFeatures(CsvParser.Feature.WRAP_AS_ARRAY)
    .readValues(after);
it.readAll().forEach(item => {
  println(item)
})

现在我想使用第一行作为标题而不是一组字符串,然后映射其他行。我创建我的模型对象,如......

@JsonIgnoreProperties(ignoreUnknown = true)
class ManagedUnit {
  @JsonProperty("Unit Name") val unitName: String = null
  def getUnitName(): String = unitName
}

然后尝试更改解析器...

val source = scala.io.Source.fromFile(path)
val after = try source.mkString finally source.close()
val mapper: CsvMapper = new CsvMapper()
val schema: CsvSchema =
  mapper.typedSchemaFor(classOf[ManagedUnit])
    .withHeader()
    .withColumnReordering(true)
val it: MappingIterator[ManagedUnit] =
  mapper.readerForListOf(classOf[ManagedUnit])
    .`with`(schema)
    .readValues(after);
it.readAll().forEach(item => {
  println(item)
})

但是当我跑步时,我得到...

引起:com.fasterxml.jackson.databind.exc.MismatchedInputException:无法java.util.ArrayList<com.purepm.data.model.appfolio.ManagedUnit>从对象值(令牌JsonToken.START_OBJECT)反序列化类型值

4

1 回答 1

0

readerForListOf本来应该readerWithTypedSchemaFor

于 2021-12-13T23:29:21.150 回答