java - 使用opencv抛出异常将字符串转换为pojo类
问题描述
我需要将我的 http 响应转换为模型,我的响应是纯 csv 格式:
try {
StringReader stringReader = new StringReader(string);
CSVReader csvReader = new CSVReaderBuilder(stringReader).build();
CsvToBean<SampleModel> csvToBean = new CsvToBeanBuilder<SampleModel>(csvReader)
.withType(SampleModel.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
arrayList =(ArrayList<SampleModel>)csvToBean.parse();
} catch (Exception e) {
System.out.println(e.getMessage());
}
这让我异常:
捕获 CSV 标头时出错!
字符串如下所示:
Name, Date
Alex, 2012-10-30
Borhan, 2012-11-05
我一直在关注这里的工作:https ://sourceforge.net/p/opencsv/bugs/196/#ee18
解决方案
If you have one or more constructors with arguments, create a no-arg constructor explicitly in the bean class. If you do not have one or more constructors with arguments, you do not need to create a no-arg constructor explicitly.
SampleModel.java:
import com.opencsv.bean.CsvBindByName;
public class SampleModel {
@CsvBindByName(column = "Name")
private String name;
@CsvBindByName(column = "Date")
private String date;
public SampleModel() {
// Empty
}
public SampleModel(String name, String date) {
this.name = name;
this.date = date;
}
public void setName(String name) {
this.name = name;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "{" + name + ", " + date + "}";
}
}
As you can see, I have a two-args constructor and therefore I need to provide a no-arg constructor explicitly. If you remove the no-arg constructor and run Main.java
(given below), it will fail at runtime.
However, the following definition of the bean does not require an explicit no-arg constructor. You can verify it by running Main.java
again with this definition.
import com.opencsv.bean.CsvBindByName;
public class SampleModel {
@CsvBindByName(column = "Name")
private String name;
@CsvBindByName(column = "Date")
private String date;
public void setName(String name) {
this.name = name;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "{" + name + ", " + date + "}";
}
}
Main.java:
import java.io.StringReader;
import java.util.List;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.bean.CsvToBeanBuilder;
public class Main {
public static void main(String[] args) {
String string = "Name, Date\n" +
"Alex, 2012-10-30\n" +
"Borhan, 2012-11-05";
try {
StringReader stringReader = new StringReader(string);
CSVReader csvReader = new CSVReaderBuilder(stringReader).build();
List<SampleModel> arrayList= new CsvToBeanBuilder<SampleModel>(csvReader)
.withType(SampleModel.class)
.withIgnoreLeadingWhiteSpace(true)
.withSkipLines(1)// Skip the header line
.build()
.parse();
System.out.println(arrayList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
[{Alex, 2012-10-30}, {Borhan, 2012-11-05}]
Original answer:
A workaround is to create an instance of the bean out of each record you read from the CSV and add the instance to the List
as shown below:
List<SampleModel> arrayList = new ArrayList<>();
String[] nextRecord;
CSVReader csvReader = new CSVReaderBuilder(stringReader)
.withSkipLines(1)// Skip the header line
.build();
while ((nextRecord = csvReader.readNext()) != null) {
arrayList.add(new SampleModel(nextRecord[0], nextRecord[1]));
}
推荐阅读
- javascript - jupyter notebook如何在浏览器中运行python进程在后台运行?
- c++ - Visual Studio 2019 类向导将继承的类放在新行上
- javascript - 如何在jquery中同时设置'draggable'和'resizable'?
- r - 根据日期(ID)分组并从 R 中的组值和组名称创建 3 个新列
- ios - Apple VOIP 通知到期不起作用
- qt - QML如何使用加载器打开新页面onClick
- javascript - React componentDidMount“解析错误:缺少分号”
- c++ - 'g++' 不是内部或外部命令、可运行程序或批处理文件。即使 gcc 安装正确
- python - Mac M1 未加载 Openssl 库(未加载库:/usr/local/opt/openssl@1.1/lib/libssl.1.1.dylib)
- c# - C# Renci SSH.NET SFTPClient - OpenSSH (Windows & CentOS) 是否可以从 sshd_config 文件中获取 ChRootDirectory 值