簡體   English   中英

傑克遜CSV的WRAP_AS_ARRAY

[英]Jackson CSV's WRAP_AS_ARRAY

http://fasterxml.github.io/jackson-dataformat-csv/javadoc/2.0.0/com/fasterxml/jackson/dataformat/csv/CsvParser.Feature.htmlWRAP_AS_ARRAY是:

決定記錄流(通常是CSV行,但在引用值中包含換行符時有時會顯示多行)的功能是公開的:作為對象序列(false)或作為對象數組(true)。

“對象序列”和“對象數組”之間有什么區別? 對我來說,描述似乎相同。

解析一系列對象:調用readValues()並獲取一個MappingIterator,它為您提供一對一的對象。 等效於包含多個JSON對象的輸入,一個接一個。

解析對象數組:調用readValue()並獲取對象列表。 等效於包含JSON數組的輸入。

例子:

@Test
public void parses_csv_to_object_list() throws Exception {
    String csv = "id,name\n1,Red\n2,Green\n3,Blue";
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(ColourData.class).withHeader();
    ObjectReader reader = mapper.readerFor(ColourData.class).with(schema);
    try (MappingIterator<ColourData> iter = reader.readValues(csv)) {
        assertThat(iter.readAll(),
                contains(new ColourData(1, "Red"), new ColourData(2, "Green"), new ColourData(3, "Blue")));
    }
}

@Test
public void parses_csv_to_object_list_in_one_read() throws Exception {
    String csv = "id,name\n1,Red\n2,Green\n3,Blue";
    CsvMapper mapper = new CsvMapper().enable(CsvParser.Feature.WRAP_AS_ARRAY);
    CsvSchema schema = mapper.schemaFor(ColourData.class).withHeader();
    ObjectReader reader = mapper.readerFor(new TypeReference<List<ColourData>>() {
    }).with(schema);
    assertThat(reader.readValue(csv),
            contains(new ColourData(1, "Red"), new ColourData(2, "Green"), new ColourData(3, "Blue")));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM