具有复杂bean结构的openCSV

问题描述 投票:2回答:1

我尝试使用openCSV映射嵌套的bean结构。我找到了@CsvRecurse批注,但是如果多次使用嵌套的bean,这似乎不起作用。

我必须解决什么选项?

示例(改编自上面链接的文档)

要映射的数据结构:

title,author1 given name,author1 surname,author2 given name,author2 surname
Space Opera 2.0,Andrew,Jones,Hanna,Smith

我想得到以下豆子

public class Book {
    @CsvBindByName
    private String title;

    // TODO: How to bind author1 and author2?
    private Author author1;
    private Author author2;

    // Accessor methods go here.
}

public class Author {
    // TODO: can I somehow use a prefix/Regex for the column here to differentiate between author1 and author2?
    @CsvBindByName(column = "author[1/2] given name")
    private String givenName;

    @CsvBindByName(column = "author[1/2] surname")
    private String surname;

    // Accessor methods go here.
}
java opencsv
1个回答
0
投票

您可以使用自定义'ColumnPositionMappingStrategy'。覆盖采用String [](csv行)并可以从中形成任何对象的'populateNewBean'方法。

Like this

''CsvRecurse'应该适用于Open OpenCSV 5.0 btw。

© www.soinside.com 2019 - 2024. All rights reserved.