简体   繁体   中英

How to import data from a csv file into a java object arraylist with the Jackson library? please

The object in question:

public class DataSupplyPoint {

    private String date;
    private double value;
    private SupplyPoint supplyPoint;

    public DataSupplyPoint(String date, double value, SupplyPoint supplyPoint) {
        super();
        this.date = date;
        this.value = value;
        this.supplyPoint = supplyPoint;
    }

Example of my csv file:

supplyPoint,date,value
0123456678,2021-03-10,2412.1999
0123456678,2021-06-10,3412.1999
0123456678,2021-09-10,4412.1999
0123456678,2021-12-10,5412.1999
0123456678,2021-03-10,2412.1999
0123456678,2021-06-10,3412.1999
0123456678,2021-09-10,4412.1999
0123456678,2021-12-10,5412.1999

What I tried to do:

public ArrayList<DataSupplyPoint> importDataSupplyPointInCSV(String fileName) {
    CsvMapper csvMapper = new CsvMapper();
    CsvSchema schema = CsvSchema.emptySchema().withHeader();
    ObjectReader oReader = csvMapper.readerFor(DataSupplyPoint.class).with(schema);
    ArrayList<DataSupplyPoint> dataSupplyPointList = new ArrayList<>();
    
    try(Reader reader = new FileReader(fileName + ".csv")) {
        MappingIterator<DataSupplyPoint> mi = oReader.readValues(reader);
        
        while (mi.hasNext()) {
            DataSupplyPoint current = mi.next();
            dataSupplyPointList.add(current);
        }
        return dataSupplyPointList;
    } catch (IOException e) {
        e.printStackTrace();
        return dataSupplyPointList;
    }       
}

It makes me make this mistake:

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of user.SupplyPoint (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('012345678912345678')'''

This is my first post, I promise to improve ahah

In the constructor of class DataSupplyPoint Jackson does not know which CSV column corresponds to which constructor parameter. Jackson cannot figure this out from the Java names of the constructor parameters. Therefore you need to tell it explicitly by annotating the parameters with @JsonProperty :

public DataSupplyPoint(@JsonProperty("date") String date,
                       @JsonProperty("value") double value,
                       @JsonProperty("supplyPoint") SupplyPoint supplyPoint) {
    super();
    this.date = date;
    this.value = value;
    this.supplyPoint = supplyPoint;
}

See also the javadoc of @JsonCreator :

NOTE: when annotating creator methods (constructors, factory methods), method must either be:

  • Single-argument constructor/factory method without JsonProperty annotation for the argument: if so, this is so-called "delegate creator", in which case Jackson first binds JSON into type of the argument, and then calls creator. This is often used in conjunction with JsonValue (used for serialization).
  • Constructor/factory method where every argument is annotated with either JsonProperty or JacksonInject , to indicate name of property to bind to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM