Java Jackson:将 csv 文件解析为包含对象列表的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12602760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:31:23  来源:igfitidea点击:

Java Hymanson: Parsing a csv file into an object containing a List of objects

javacsvHymanson

提问by DanF7

I am trying to parse a csv file using Hymanson CsvParser into an object that also contains a list of another class.

我正在尝试使用 Hymanson CsvParser 将 csv 文件解析为一个对象,该对象还包含另一个类的列表。

So the first 2 columns contain data that needs to be bound to the parent class and the data afterwards would need to be bound to another class.

所以前两列包含需要绑定到父类的数据,之后的数据需要绑定到另一个类。

public class Person {
    private String name;
    private String age;
    private List<CarDetails> carDetails;

    //Getters+setters
}

public class CarDetails {
    private String carMake;
    private String carRegistration;

    //Getters+setters
}

The log to be parsed would look like:

要解析的日志如下所示:

John Doe, 30, Honda, D32GHF

Or in another log of users all with 2 cars it may look like:

或者在另一个所有拥有 2 辆车的用户日志中,它可能看起来像:

Jane Doe, 29, Mini, F64RTZ, BMW, T56DFG 

To parse the initial 2 items of data to the "Person" class is no problem.

将最初的 2 项数据解析到“Person”类是没有问题的。

CsvMapper mapper = new CsvMapper();
CsvSchema schema = CsvSchema.builder()
    .addColumn("name")
    .addColumn("age")

for(numberOfCars=2; numberOfCars!=0 ; numberOfCars--)
    schema = schema.rebuild()
        .addColumn("carMake")
        .addColumn("carRegistration")

MappingIterator<Map.Entry> it = mapper
    .reader(Person.class)
    .with(schema)
    .readValues(personLog);
    List<Person> people = new ArrayList<Person>();
    while (it.hasNextValue()) {
        Person person = (Person) it.nextValue();
        people.add(person);
    }

But I do not know how to parse the CarDetails. It seems like I have to be able to search for the value, create a new CarDetails object and add it to the Person object, but I can't see how to extract that information.

但我不知道如何解析 CarDetails。似乎我必须能够搜索该值,创建一个新的 CarDetails 对象并将其添加到 Person 对象,但我看不到如何提取该信息。



My solution is the following:

我的解决方案如下:

List<Person> people = new ArrayList<Person>();

MappingIterator<Map<?,?>> it = mapper.reader(schema).withType(Map.class).readValues(personLog);
while(it.hasNextValue()) {
    Person person = new Person();
    CarDetails = new CarDetails();
    Map<?,?> result = it.nextValue();
    person.setName(result.get("name").toString());
    carDetails.setCarMake(result.get("carMake").toString());
    person.addCarDetails(carDetails);
    people.add(person);
}    

采纳答案by David Grant

If you look at the Javadoc for CsvMapper.readerWithSchemaFor, it states:

如果您查看CsvMapper.readerWithSchemaFor的 Javadoc ,它会指出:

no CSV types can be mapped to arrays or Collections

没有 CSV 类型可以映射到数组或集合