簡體   English   中英

使用jackson-csv解析雙精度值時發生異常

[英]Exception while parsing a double value with jackson-csv

我在使用fastxml從CSV文件解析雙精度值時遇到一些問題。 CSV文件以標題ID;Size;...開頭,下面的行是1;27.98;...還有一個模型可以映射CSV文件:

class Model{
   private String id;
   private double size;
   //...
}

此外,我已按照教程頁面上的說明配置了CSV分析器:

//...
CsvSchema schema = CsvSchema.builder().addColumn("id").addColumn("size")
     .setColumnSeparator(';')
     .setEscapeChar('"')
     .build();
CsvMapper mapper = new CsvMapper();
MappingIterator<Model> it = mapper.reader(Model.class).with(schema)
    .readValues(new File(csvFile));
while(it.hasNext()){
    Model row = it.next();
    System.out.println(row);
}

當我運行解析器時,“ id”會被正確解析,但是編譯器會拋出異常,並顯示消息RuntimeJsonMappingException: Can not construct an instance of double from String value 'size':not a valid Double value

我不明白問題出在哪里,因為值27.98是有效的雙精度值。

我在配置中缺少什么嗎?

.withHeader()上使用.withHeader()CsvMapper所示:

CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(Model.class).withHeader();

然后,您可以將標題保留在csv文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM