如何使用 CsvMapper 将 CSV 映射到带有列表的 Object?
How to map a CSV to Object with List using CsvMapper?
我正在尝试使用 CsvMapper 将 Csv 文件映射到 Object 的 Object 实例列表,这可能吗?
示例:
Csv 文件的内容(没有 header):
字段 1:姓名
字段 2:性别
字段 3:爱好(列表以逗号分隔)
字段 4:关系
Bob;male;riding,swimming,dance;single
示例class:
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Class User {
String name;
String gender;
List<Hobby> hobbys;
String relation;
Boolean check;
int index;
public static User from(String name,
String gender,
List<Hobby> hobbys,
String relation) {
------ SOME LOGIC -----
return new User(name,
gender,
hobbys,
relation,
index)
}
}
Class UserCreator{
public List<User> create(File csvFile){
//Now my Problem how can I map the csvFile to User Object???
//my attempts
CsvMapper csvMapper = new CsvMapper();
CsvSchema csvSchema = CsvSchema
.emptySchema()
.withoutHeader()
.withColumnSeparator(',')
.withLineSeparator(";");
try {
//here its crashes, my problem ist how can I map it to the User object?
MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class)
.with(csvSchema)
.readValues(file);
System.out.println("test");
} catch (IOException e) {
e.printStackTrace();
}
}
}
提前致谢。
我找到了一个解决方案:
我使用 common-cdv (apache) 作为插件而不是 Jackson CsvMapper。
我的解决方案:
public List<User> create(File csvFile){
Reader reader = new FileReader(file);
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withDelimiter(';').withIgnoreEmptyLines());
csvParser.forEach(csvRecord -> {
String name = csvRecord.get(0);
String gender = csvRecord.get(1);
List<Hobby> hobbys = List.of(csvRecord.get(2).split(','));
String relation = csvRecord.get(3);
.......
});
csvParser.close();
}
我正在尝试使用 CsvMapper 将 Csv 文件映射到 Object 的 Object 实例列表,这可能吗?
示例:
Csv 文件的内容(没有 header):
字段 1:姓名
字段 2:性别
字段 3:爱好(列表以逗号分隔)
字段 4:关系
Bob;male;riding,swimming,dance;single
示例class:
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Class User {
String name;
String gender;
List<Hobby> hobbys;
String relation;
Boolean check;
int index;
public static User from(String name,
String gender,
List<Hobby> hobbys,
String relation) {
------ SOME LOGIC -----
return new User(name,
gender,
hobbys,
relation,
index)
}
}
Class UserCreator{
public List<User> create(File csvFile){
//Now my Problem how can I map the csvFile to User Object???
//my attempts
CsvMapper csvMapper = new CsvMapper();
CsvSchema csvSchema = CsvSchema
.emptySchema()
.withoutHeader()
.withColumnSeparator(',')
.withLineSeparator(";");
try {
//here its crashes, my problem ist how can I map it to the User object?
MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class)
.with(csvSchema)
.readValues(file);
System.out.println("test");
} catch (IOException e) {
e.printStackTrace();
}
}
}
提前致谢。
我找到了一个解决方案: 我使用 common-cdv (apache) 作为插件而不是 Jackson CsvMapper。
我的解决方案:
public List<User> create(File csvFile){
Reader reader = new FileReader(file);
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withDelimiter(';').withIgnoreEmptyLines());
csvParser.forEach(csvRecord -> {
String name = csvRecord.get(0);
String gender = csvRecord.get(1);
List<Hobby> hobbys = List.of(csvRecord.get(2).split(','));
String relation = csvRecord.get(3);
.......
});
csvParser.close();
}