本文介绍了csv与另一个pojo转换为pojo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Orgnization{

private String name;
private String uniqueId;
private boolean selfRegEnabled;
private List<Address> addrList;

public void setAddress(Address a){..}
public void setName(String name){..}

}

Addess{
private String type;
private String line1;
private String line2;
private String line3;
private String city;
private String state;
private String zip;
private String country;
}

CSV标题列如下

System.UniqueID,名称,EnableSelf-Registration,Addr1.Type,Addr1.Line1,Addr1.Line2,Addr1.Line3,Addr1.City,Addr1.State,Addr1.Zip,Addr1.Country,Addr2.Type,Addr2 .Line1,Addr2.Line2,Addr2.Line3,Addr2.City,Addr2.State,Addr2.Zip,Addr2.Country,Addr3.Type,Addr3.Line1,Addr3.Line2,Addr3.Line3,Addr3.City,Addr3.State ,Addr3.Zip,Addr3.Country

System.UniqueID,Name,EnableSelf-Registration,Addr1.Type,Addr1.Line1,Addr1.Line2,Addr1.Line3,Addr1.City,Addr1.State,Addr1.Zip,Addr1.Country,Addr2.Type,Addr2.Line1,Addr2.Line2,Addr2.Line3,Addr2.City,Addr2.State,Addr2.Zip,Addr2.Country,Addr3.Type,Addr3.Line1,Addr3.Line2,Addr3.Line3,Addr3.City,Addr3.State,Addr3.Zip,Addr3.Country

我的问题可能与以下链接有关

My question might be related to below link

OpenCSV CSV到JavaBean

我没有看到该线程有正确的答案(我不确定是否从该线程中错过了任何答案)

I didn't see that thread has a proper answer (I am not sure if I miss any from that thread)

我们可以使用任何现有的csv库(例如supercsv,opencsv)来实现相同的目的吗?

Can we achieve same thing with any of the existing csv libraries such as supercsv, opencsv?

如果我使用的是supercsv,是否可以将csv的System.UniqueID列映射到我的bean的systemUniqueID属性

推荐答案

您当然可以使用CsvDozerBeanReader使用Super CSV进行此操作.在网站上查看此示例.

You can certainly do this with Super CSV using CsvDozerBeanReader. See this example on the website.

SO答案中也对此进行了更详细的说明.

It's also explained in a bit more detail on this SO answer.

您可能还对这个最近的问题感兴趣,因为它演示了使用以下方法实现深度/索引映射的不同方法超级CSV(无论是否使用推土机).

You may also be interested in this recent question, as it demonstrates the different ways to achieve deep/indexed mapping with Super CSV (with and without using Dozer).

按照网站上的CsvDozerBeanReader示例,要从您的问题中读取CSV,您将使用以下字段映射:

Following the CsvDozerBeanReader example on the website, to read the CSV from your question you would use a field mapping of:

final String[] fieldMapping = new String[]{
    "uniqueId",
    "name",
    "selfRegEnabled",
    "addrList[0].type",
    "addrList[0].line1",
    "addrList[0].line2",
    "addrList[0].line3",
    "addrList[0].city",
    "addrList[0].state",
    "addrList[0].zip",
    "addrList[0].country",
    "addrList[1].type",
    "addrList[1].line1",
    "addrList[1].line2",
    "addrList[1].line3",
    "addrList[1].city",
    "addrList[1].state",
    "addrList[1].zip",
    "addrList[1].country",
    "addrList[2].type",
    "addrList[2].line1",
    "addrList[2].line2",
    "addrList[2].line3",
    "addrList[2].city",
    "addrList[2].state",
    "addrList[2].zip",
    "addrList[2].country"
};

此外,由于selfRegEnabled字段是布尔值,因此您需要使用单元处理器将String值转换为布尔值-为此,您将使用ParseBool处理器.

Also, because the selfRegEnabled field is a boolean, you'll need to use cell processors to transform the String value into a Boolean - to do this you'd use the ParseBool processor.

这篇关于csv与另一个pojo转换为pojo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:12