1

I'm trying to write a code where a csv content is converted to a bean. This is how my code looks like

final String TEMPLATE_FILE="addresses.csv";

    CSVReader template = new CSVReader(new FileReader(TEMPLATE_FILE));
    String [] header = template.readNext();
    ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
    strat.setType(PersonEx.class);
    strat.setColumnMapping(header);
    CsvToBean csv = new CsvToBean();
    List list = csv.parse(strat, template);

In here if a particular attribute of PersonEx.class is missing in the csvHeader that attribute is ascribed as null. Is there a way, I can ignore this and only create a bean of values present in the header?

3
  • What is wrong with the new bean containing null values for the attributes not present in the CSV file? Do you want those attributes to have some other default value? Commented Mar 24, 2013 at 18:20
  • As I'm converting the bean to a JSON Object, which is my original requirement. I want the JSON object to only contain the CSV header fields and nothing else. I'm thinking once I conver this to a jsonNode using jackson, is there anyway I can remove the elements from jsonNode using there string fields? I've asked the question here. stackoverflow.com/questions/15603033/… Commented Mar 24, 2013 at 19:39
  • I would create a bean with only the attributes in your CSV file. Commented Mar 24, 2013 at 22:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.