相关疑难解决方法(0)

使用OpenCSV将CSV解析为多个/嵌套bean类型?

我有各种CSV,包含一些标准列和一些完全随机的字段:

firstname, lastname, dog_name, fav_hat, fav_color
bill,smith,fido,porkpie,blue
james,smith,rover,bowler,purple


firstname, lastname, car_type, floor_number
tom, collins, ford, 14
jim, jones, toyota, 120
Run Code Online (Sandbox Code Playgroud)

所以我试图将它们解析为Person.class bean,它包含firstname和lastname,然后我有一个名为PersonAttribute.class的第二个类来保存......还有其他什么.

这两个类的讽刺大纲:

class Person {
 public String firstname;
 public String lastname;
 public List<PersonAttribute> attribs;
}

class PersonAttribute {
 public Person p;
 public String key; // header name, ex. 'car_type'
 public String value; // column value, ex. 'ford'
}
Run Code Online (Sandbox Code Playgroud)

我一直在opencsv中使用CsvToBean函数:

public static List<Person> parseToBeans(File csvFile, HashMap<String, String> mapStrategy, Class beanClass) throws IOException {
    CSVReader reader = null;
    try {
        reader …
Run Code Online (Sandbox Code Playgroud)

java opencsv supercsv

6
推荐指数
1
解决办法
8370
查看次数

标签 统计

java ×1

opencsv ×1

supercsv ×1