Univocity-将单行解析为多个bean

时间:2019-05-30 18:35:10

标签: univocity

是否可以根据索引范围将单行解析为多个bean

示例

行:“ field1”,“ field2”,“ field3”,....,“ field9”

class ColumnsOneAndTwo {
   protected String field1;
   protected String field2;
}

class ColumnThreeAndNine {
   protected String field3;
   protected String field9;
}

class Row {

  @Parsed(indexes = 0, 1)
  protected ColumnOneAndTwo fields;

  @Parsed(indexes = 2, 8)
  protected ColumnThreeAndNine moreFields;

} 

BeanListProcessor<Row> rowProcessor = new BeanListProcessor<Row>(Row.class);

CsvRoutines routines = new CsvRoutines(parserSettings);

for (Row data : routines.iterate(Row.class, <file>, "UTF-8")) {  

}

1 个答案:

答案 0 :(得分:0)

您正在寻找@Nested注释。只需使用:

class ColumnsOneAndTwo {
    @Parsed(index=1)
    protected String field1;

    @Parsed(index=2)
    protected String field2;
}

class ColumnThreeAndNine {
    @Parsed(index=3)
    protected String field3;

    @Parsed(index=9)
    protected String field9;
}

class Row {

   @Nested
   protected ColumnOneAndTwo fields;

   @Nested
   protected ColumnThreeAndNine moreFields;

} 

希望有帮助。

免责声明:我是这个图书馆的作者。它是开源且免费的(Apache 2.0许可证)