在Java中使用openCSV解析带双引号的CSV文件的问题

时间:2019-03-10 16:41:09

标签: java csv parsing opencsv

我想的问题与引号-“”及其之间的逗号有关。

我有一个 .csv ,其中包含来自IMDB网站的下一个数据:

  

tt0100150,7,2011-12-26,米勒的穿越,https://www.imdb.com/title/tt0100150/,movie,7.8,115,1990,“犯罪,戏剧,惊悚片”,117091,1990-09-21,“乔尔·科恩,伊桑·科恩”   tt1001508,6,2010-10-24,他只是不那么喜欢你,https://www.imdb.com/title/tt1001508/,movie,6.4,129,2009,“喜剧,戏剧,浪漫”,148405,2009-01-24,肯·卡皮斯   tt0100405,6,2010-02-09,Pretty Woman,https://www.imdb.com/title/tt0100405/,movie,7.0,119,1990,“喜剧,浪漫”,256191,1990-03-23,Garry Marshall   tt0100419,6,2010-03-21,问题儿童,https://www.imdb.com/title/tt0100419/,movie,5.3,81,1990,“喜剧,家庭”,25671,1990-07-27,Dennis Dugan   tt0100502,6,2010-06-18,RoboCop 2,https://www.imdb.com/title/tt0100502/,movie,5.8,117,1990,“动作,犯罪,科幻,惊悚片”,74631,1990-06-22,Irvin Kershner   tt0100507,6,2010-02-24,Rocky V,https://www.imdb.com/title/tt0100507/,movie,5.2,104,1990,“戏剧,戏剧”,113905,1990-11-13,John G. Avildsen

我想使用openCSV将其解析为Bean对象:

        CSVReader csvReader = new CSVReader(new FileReader(dbFile), ',', '"', 1);
    ColumnPositionMappingStrategy mappingStrategy =
            new ColumnPositionMappingStrategy();
    mappingStrategy.setType(Movie.class);
    String[] columns = new String[]{"id", "rating", "dateRated", "title", "url", "type",
            "imdbRating", "runtime", "year", "genres", "votes", "releaseDate", "directors"};
    mappingStrategy.setColumnMapping(columns);
    CsvToBean ctb = new CsvToBean();
    List list = ctb.parse(mappingStrategy, csvReader);

    for (Object object : list) {
        Movie movie = (Movie) object;
        System.out.println(movie)

Bean类具有所有必填字段:

class Movie {
private String id;
private String rating;
private String dateRated;
private String title;
private String url;
private String type;
private String imdbRating;
private String runtime;
private String year;
private String genres;
private String votes;
private String releaseDate;
private String directors;

//getters and setters.

但是我遇到下一个错误:

  

java.lang.RuntimeException:java.lang.IllegalAccessException:类com.opencsv.bean.AbstractMappingStrategy无法访问带有修饰符“”的类parse.Movie的成员。

有没有办法解析这样的文本:

  

“犯罪,戏剧,惊悚片”

作为一个字符串?

0 个答案:

没有答案