解析CSV时遇到未识别字段错误消息。

huangapple go评论76阅读模式
英文:

Getting urecognized field error message when parsing csv

问题

我正在尝试使用Jackson库将CSV文件转换为JSON。我已经创建了与CSV文件标题类似的POJO类属性。但是即使如此,仍然遇到无法识别的字段错误问题

以下是我的代码:

public static List<SalesValidationDetails> parseCSVFile(File file) throws IOException {
            
    CsvMapper csvMapper = new CsvMapper();
    MappingIterator<SalesValidationDetails> eachSale = 
        csvMapper.readerWithTypedSchemaFor(SalesValidationDetails.class).readValues(file);
    return eachSale.readAll();
}

POJO类:

@JsonPropertyOrder({ "OrderReference", "Date", "Status", "StatusNote", "NewSalePrice", "TransactionPartsNew", "Currency"})
public class SalesValidationDetails {

    private String OrderReference;
    private String Date;
    private String Status;
    private String StatusNote;
    private String NewSalePrice;
    private String TransactionPartsNew;
    private String Currency;
    
    // 省略了getter和setter方法

    @Override
    public String toString() {
        return "SalesValidationDetails{" +
            "orderReference='" + OrderReference + '\'' +
            ", date='" + Date +
            ", status='" + Status + '\'' +
            ", statusNote='" + StatusNote + '\'' +
            ", newSalesPrice='" + NewSalePrice + '\'' +
            ", transactionPartsNew='" + TransactionPartsNew + '\'' +
            ", currency='" + Currency + '\'' +
            '}';
    }
}

我的CSV文件如下所示:

OrderReference,Date,Status,StatusNote,NewSalePrice,TransactionPartsNew,Currency
ORD123,2023-08-30,Processed,All good,150.00,Part A,USD
ORD124,2023-08-31,Shipped,On its way,200.00,Part B,EUR

我的错误消息:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "transactionPartNew" (class com.decathlon.beans.SalesValidationDetails), not marked as ignorable (7 known properties: "currency", "newSalePrice", "statusNote", "orderReference", "date", "status", "transactionPartsNew"])
 at [Source: (com.fasterxml.jackson.dataformat.csv.impl.UTF8Reader); line: 1, column: 94] (through reference chain: com.decathlon.beans.SalesValidationDetails["transactionPartNew"])

有人可以帮助我找出问题出在哪里吗?我已经尝试了所有方法。

英文:

I am trying to convert CSV file to JSON using Jackson library. I have created the POJO class attributes similar to CSV file headers. But even though facing unrecognized field error problem.

Here is my code

    public static List&lt;SalesValidationDetails&gt; parseCSVFile(File file) throws IOException {
                
          CsvMapper csvMapper = new CsvMapper();
          MappingIterator&lt;SalesValidationDetails&gt; eachSale = 
          csvMapper.readerWithTypedSchemaFor(SalesValidationDetails.class).readValues(file);
    return eachSale.readAll();

}

Pojo class:

@JsonPropertyOrder({ &quot;OrderReference&quot;, &quot;Date&quot;, &quot;Status&quot;, &quot;StatusNote&quot;, &quot;NewSalePrice&quot;, &quot;TransactionPartsNew&quot;, &quot;Currency&quot;})
public class SalesValidationDetails {

    private String OrderReference;
    private String Date;
    private String Status;
    private String StatusNote;
    private String NewSalePrice;
    private String TransactionPartsNew;
    private String Currency;
    
    public String getOrderReference() {
    return OrderReference;
}

public String getDate() {
    return Date;
}

public String getStatus() {
    return Status;
}

public String getStatusNote() {
    return StatusNote;
}

public String getNewSalePrice() {
    return NewSalePrice;
}

public String getTransactionPartNew() {
    return TransactionPartsNew;
}

public String getCurrency() {
    return Currency;
}

public void setOrderReference(String OrderReference) {
    OrderReference = OrderReference;
}

public void setDate(String Date) {
    Date = Date;
}

public void setStatus(String Status) {
    Status = Status;
}

public void setStatusNote(String StatusNote) {
    StatusNote = StatusNote;
}

public void setNewSalePrice(String NewSalePrice) {
    NewSalePrice = NewSalePrice;
}

public void setTransactionPartsNew(String TransactionPartsNew) {
    TransactionPartsNew = TransactionPartsNew;
}

public void setCurrency(String Currency) {
    Currency = Currency;
}

@Override
public String toString() {
    return &quot;SalesValidationDetails{&quot; +
            &quot;orderReference=&#39;&quot; + OrderReference + &#39;\&#39;&#39; +
            &quot;, date=&quot; + Date +
            &quot;, status=&#39;&quot; + Status + &#39;\&#39;&#39; +
            &quot;, statusNote=&#39;&quot; + StatusNote + &#39;\&#39;&#39; +
            &quot;, newSalesPrice=&#39;&quot; + NewSalePrice + &#39;\&#39;&#39; +
            &quot;, transactionPartsNew=&#39;&quot; + TransactionPartsNew + &#39;\&#39;&#39; +
            &quot;, currency=&#39;&quot; + Currency + &#39;\&#39;&#39; +
            &#39;}&#39;;
}

}

My CSV file looks like this:
解析CSV时遇到未识别字段错误消息。

My error message:

Exception in thread &quot;main&quot; com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field &quot;transactionPartNew&quot; (class com.decathlon.beans.SalesValidationDetails), not marked as ignorable (7 known properties: &quot;currency&quot;, &quot;newSalePrice&quot;, &quot;statusNote&quot;, &quot;orderReference&quot;, &quot;date&quot;, &quot;status&quot;, &quot;transactionPartsNew&quot;])
 at [Source: (com.fasterxml.jackson.dataformat.csv.impl.UTF8Reader); line: 1, column: 94] (through reference chain: com.decathlon.beans.SalesValidationDetails[&quot;transactionPartNew&quot;])

Can somebody help me what is wrong here, i have tried all the ways.

答案1

得分: 1

你的 settlers 实现错误,应该是(仅针对一个 setter 的示例):

...

public void setTransactionPartsNew(String TransactionPartsNew) {
    this.TransactionPartsNew = TransactionPartsNew;
}
...

因此,你需要为对象的字段赋值。

英文:

Your settlers implemented wrongly, it should be (example for one setter only):

...

public void setTransactionPartsNew(String TransactionPartsNew) {
    this.TransactionPartsNew = TransactionPartsNew;
}
...

So, you have to assign value to the object’s field.

huangapple
  • 本文由 发表于 2020年8月27日 23:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63619170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定