gpt4 book ai didi

java - 使用自定义分隔符列解析 CSV 未正确映射到 POJO

转载 作者:行者123 更新时间:2023-12-02 10:20:19 40 4
gpt4 key购买 nike

使用自定义列分隔符“;”解析 CSV 时并使用:

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

我收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class Author] from String value ('Email;FirstName;LastName'); no single-String constructor/factory method

这是我的解析代码:

public static <T> List<T> loadObjectList(Class<T> type, String fileName) {
CsvSchema bootstrapSchema = CsvSchema.emptySchema();
bootstrapSchema.withColumnSeparator(';').withoutQuoteChar();
CsvMapper mapper = new CsvMapper();
File file = new File(fileName);
MappingIterator<T> readValues =
mapper.reader(type)
.with(bootstrapSchema)
.readValues(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
return readValues.readAll();
}

这是我想要映射到 CSV 的模型:

@JsonPropertyOrder({ "Email", "FirstName", "LastName" })
public class Author {

String Email;
String FirstName;
String LastName;

public String getEmail() {
return Email;
}

public void setEmail(String Email) {
this.Email = Email;
}

public String getFirstName() {
return FirstName;
}

public void setFirstName(String firstName) {
FirstName = firstName;
}

public String getLastName() {
return LastName;
}

public void setLastName(String lastName) {
LastName = lastName;
}

public Author(String email, String firstName, String lastName) {
Email = email;
FirstName = firstName;
LastName = lastName;
}

}

最佳答案

CsvSchema bootstrapSchema = CsvSchema.emptySchema();
bootstrapSchema.withColumnSeparator(';').withoutQuoteChar();

我不确定这部分 - 它不是构建器模式。

尝试

CsvSchema bootstrapSchema = CsvSchema.emptySchema().bootstrapSchema.withColumnSeparator(';').withoutQuoteChar();

相反。

关于java - 使用自定义分隔符列解析 CSV 未正确映射到 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54388023/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com