Loading... # Jackson 序列化对象为 CSV 文件 ## 依赖 ```xml <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-csv --> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-csv</artifactId> <version>2.14.0-rc3</version> </dependency> ``` ## 序列化 ```kotlin val zhiPinFile = File("...") val jobList = ArrayList<Job>() val csvMapper : CsvMapper = CsvMapper() // schemaFor(Job::class.java) 需要序列化的对象类 // withHeader() 输出到csv文件第一行,不加则没有 val csvSchema : CsvSchema = csvMapper.schemaFor(Job::class.java).withHeader() csvMapper.writer(csvSchema).writeValues(zhiPinFile).write(jobList) ``` ## 元素排序 添加注解,不加默认按照首字母排序 ```kotlin @JsonPropertyOrder("name","description") data Job class(val name : String, val description : String){} ``` <br> 参考: [How to serialize object to CSV file?](https://stackoverflow.com/questions/3666007/how-to-serialize-object-to-csv-file) [Jackson JsonMappingException: Unrecognized column 'C': known columns {"A","B"}](https://stackoverflow.com/questions/45076150/jackson-jsonmappingexception-unrecognized-column-c-known-columns-a-b) [jackson-data-format-csv 读取csv文件——高级配置及原理解析](https://blog.csdn.net/xiaofeixiaqing/article/details/78310073) 最后修改:2022 年 11 月 04 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 2 本作品采用 CC BY-NC-SA 4.0 International License 进行许可。