Я бы использовал поток из root элементов узла и отобразил бы каждый элемент в sting в формате csv. Примерно так:
package example;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.google.common.collect.Streams;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
public class CsvMapperTest {
@Test
public void csvMapper() throws Exception{
String [][] names = {
{"1", "John", "Lennon"},
{"2", "Bob", "Marley"}
};
JsonNode jsonNode = new ObjectMapper().valueToTree(names);
CsvSchema s = CsvSchema.builder()
.addColumn("id", CsvSchema.ColumnType.NUMBER_OR_STRING)
.addColumn("name", CsvSchema.ColumnType.STRING)
.addColumn("lastname", CsvSchema.ColumnType.STRING)
.build();
ObjectWriter csvWriter = new CsvMapper().writer(s);
Stream<String> csvStream = Streams.stream(jsonNode.elements()).map(el -> {
try {
return csvWriter.writeValueAsString(el);
} catch (Exception ex) {
return null;
}
}
);
List<String> lines =csvStream.collect(Collectors.toList());
assertThat(lines, hasSize(2));
String line0 = lines.get(0).trim();
assertThat(lines.get(0).trim(), equalTo("1,John,Lennon"));
}
}