我正在用骆驼写一个CSV分离器。我的要求是这些:
示例csv1:
header1, header2, header3
value11, value12 , value13
value21, value22 , value23
预期拆分:
{"header1":"value11", "header2": "value12", "header3": "value13"},
{"header1":"value21", "header2": "value22", "header3": "value23"}
示例csv2:
header1, header2
value11, value12
value21, value22
预期拆分:
{"header1":"value11", "header2": "value12"},
{"header1":"value21", "header2": "value22"}
如何使用Bindy或BeanIO在驼峰中实现这一目标?
答案 0 :(得分:0)
我们可以使用以下代码。
byte[] bytes = exchange.getIn().getBody(byte[].class);
InputStream inputStream = new ByteArrayInputStream(bytes);
Reader in = new InputStreamReader(inputStream, "UTF-8");
Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in);
for (CSVRecord csvRecord : records) {
String header1= csvRecord.get("header1");
String header2 = csvRecord.get("header2");
// We can form json here
System.out.println("header1 :"+header1);
System.out.println("header2 :"+header2);
}