【问题标题】:Why does my CSVMapper put quotes around some Strings and not others?为什么我的 CSVMapper 在一些字符串周围加上引号而不是其他字符串?
【发布时间】:2018-03-08 02:10:51
【问题描述】:

我有这样的课:

@JsonPropertyOrder({"Chilli"})
public interface ChilliReport {
    @JsonProperty("Chilli")
    String getChilli();

    @JsonProperty("Price")
    String getPrice();
}

// Then, I'll populate this with some values.


CsvMapper csvMapper = new CsvMapper();
CsvSchema schema = csvMapper.schemaFor(ChilliReport.class).withHeader();

String payload = csvMapper.writer(schema).writeValueAsString(reports);
System.out.println(payload);

为什么有时会引用字段“chilli”,而有时却不会?

例如:有时,我的数据如下所示:

“辣椒”“价格”

“红色”,10


其他时间是这样的: “辣椒”“价格”

黄色,10

顺便说一句,我更改了类的名称和字段的实际名称,但这是我的问题的要点

【问题讨论】:

    标签: java csv file-io opencsv supercsv


    【解决方案1】:

    如果字段包含分隔符,则必须用引号引起来。

    有些作者可能会引用所有字段,有些只包含内部分隔符(实际上使文件更小)。

    所以,你可能有这样的事情:

    Chilli,Price
    "Red,extra",10
    Yellow,10
    

    给出下表:

    Chilli          Price
    Red,extra       10
    Yellow          10
    

    没有引号你会得到:

    Chilli          Price
    Red             extra       10
    Yellow          10
    

    或者,字符串的前面或结尾可能有空格,作者决定引用这些空格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      相关资源
      最近更新 更多