英文:
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<SalesValidationDetails> parseCSVFile(File file) throws IOException {
CsvMapper csvMapper = new CsvMapper();
MappingIterator<SalesValidationDetails> eachSale =
csvMapper.readerWithTypedSchemaFor(SalesValidationDetails.class).readValues(file);
return eachSale.readAll();
}
Pojo class:
@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;
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 "SalesValidationDetails{" +
"orderReference='" + OrderReference + '\'' +
", date=" + Date +
", status='" + Status + '\'' +
", statusNote='" + StatusNote + '\'' +
", newSalesPrice='" + NewSalePrice + '\'' +
", transactionPartsNew='" + TransactionPartsNew + '\'' +
", currency='" + Currency + '\'' +
'}';
}
}
My error message:
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"])
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论