超级csv嵌套bean [英] super csv nested bean

查看:153
本文介绍了超级csv嵌套bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv

id,name,description,price,date,name,address
1,SuperCsv,Write csv file,1234.56,28/03/2016,amar,jp nagar

我想要读取它并将其存储到json文件中。
我已经创建了两个bean课程(id,name,description,price,date)和person(name,address)

I want to read it and store it to json file. I have created two bean course(id,name,description,price,date) and person(name,address)

读取bean读者我无法设置人员地址。
(美化)输出

on reading by bean reader i'm not able to set the person address. The (beautified) output is

Course [id=1,
        name=SuperCsv,
        description=Write csv file,
        price=1234.56,
        date=Mon Mar 28 00:00:00 IST 2016,
        person=[
            Person [name=amar, address=null],
            Person [name=null, address=jpnagar]
        ]
]

我希望地址设置名称

我的代码:

public static void readCsv(String csvFileName) throws IOException {

        ICsvBeanReader beanReader = null;
        try {
            beanReader = new CsvBeanReader(new FileReader(csvFileName), CsvPreference.STANDARD_PREFERENCE);

            // the header elements are used to map the values to the bean (names must match)
            final String[] header = beanReader.getHeader(true);
            final CellProcessor[] processors = getProcessors();

            final String[] fieldMapping = new String[header.length];

            for (int i = 0; i < header.length; i++) {
                if (i < 5) {
                    // normal mappings
                    fieldMapping[i] = header[i];

                } else {
                    // attribute mappings
                    fieldMapping[i] = "addAttribute";

                }}
            ObjectMapper mapper=new ObjectMapper();
            Course course;
            List<Course> courseList=new ArrayList<Course>();
            while ((course = beanReader.read(Course.class, fieldMapping, processors)) != null) {
                // process course
                System.out.println(course);
                courseList.add(course);


            }
private static CellProcessor[] getProcessors(){

        final CellProcessor parsePerson = new CellProcessorAdaptor() {
            public Object execute(Object value, CsvContext context) {
                return new Person((String) value,null);
            }
        };  

        final CellProcessor parsePersonAddress = new CellProcessorAdaptor() {
            public Object execute(Object value, CsvContext context) {
                return new Person(null,(String) value);
            }
        };

        return new CellProcessor[] {
                new ParseInt(),
                new NotNull(),
                new Optional(),
                new ParseDouble(),
                new ParseDate("dd/MM/yyyy"),
                new Optional(parsePerson),
                new Optional(parsePersonAddress)        
        };


推荐答案

SuperCSV是我见过的第一个可以让你的解析器在对象中创建一个对象。

SuperCSV is the first parser I have seen that lets you create an object within an object.

对于你想要的东西,你可以尝试使用Apache Commons CSV或openCSV(CSVToBean)进行映射,但要做到这一点,你需要拥有内部类的setter(setName,外部类中的setAddress)所以CSVToBean将其拾取。这可能会也可能不会奏效。

for what you are wanting you can try Apache Commons CSV or openCSV (CSVToBean) to map but to do this you need to have the setters of the inner class (setName, setAddress) in the outer class so the CSVToBean to pick it up. That may or may not work.

我通常告诉人们的是一个普通的POJO,其中包含csv中的所有字段 - 数据传输对象。让解析器创建,然后使用实用程序/构建器类将普通POJO转换为所需的嵌套POJO。

What I normally tell people is to have a plain POJO that has all the fields in the csv - a data transfer object. Let the parser create that then use a utility/builder class convert the plain POJO into the nested POJO you want.

这篇关于超级csv嵌套bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆