CsvMapper在将列表写入json文件

时间:2016-04-05 05:20:15

标签: java jackson

我有一个csv文件,我需要将它绑定到bean并将其写入json文件。我正在使用jackson fasterxml。它完全读取数据但是在使用CsvMapper将其写入json时它给出异常 -

com.fasterxml.jackson.databind.JsonMappingException:无法识别的列'name':

我用getter和setter创建了一个简单的两个字段名称和地址的pojo类。

    public class Bean {

    private String name;
    private String address;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getAddress() {
    return address;
    }
    public void setAddress(String address) {
    this.address = address;
    }
    @Override
    public String toString() {
    return "Bean [name=" + name + ", address=" + address + "]";
    }}

    //in main method
    CsvMapper mapper = new CsvMapper();
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false);


    CsvSchema schema = mapper.schemaFor(Bean.class).withHeader();

    QueAnsRead bean=null;
    MappingIterator<Map.Entry> it = mapper
            .readerFor(Bean.class)
            .with(schema)
            .readValues(csvFile);
            List<Bean> alist = new ArrayList<Bean>();
            while (it.hasNextValue()) {
                 beanRead = (Bean) it.nextValue();
                alist.add(beanRead);
            }

            for(Bean read:alist){
                System.out.println(read);
            }

         mapper.writeValue(new File("c:/testCSV/data.json"), alist);

    }

0 个答案:

没有答案