Java SOJO CSV 序列化

发布于 2024-10-15 09:08:35 字数 1468 浏览 1 评论 0原文

我正在尝试使用 SOJO 将 Java 对象序列化为 CSV。该示例看起来非常简单:

Car car = new Car("My Car");
car.setDescription("This is my car.");
Serializer csvSerializer = new CsvSerializer();
String csvStr = (String) csvSerializer.serialize(car);
System.out.println(csvStr);
// print:
// description,build,properties,name,~unique-id~,class  
// This is my car.,,,My Car,0,test.net.sf.sojo.model.Car

我尝试实现我自己的示例版本。我制作了一个非常简单的 Car 类,其中包含两个 String 字段(builddescription),它实现了 setDescription(..) 方法。

这就是我实现的:

import net.sf.sojo.interchange.csv.CsvSerializer;


public class Main {

private class Car
{
    private String build;
    private String description;
    public Car(String build) {
        this.build = build;
        this.description = null;
    }

    public void setDescription(String description) {
        this.description = description;
    }


}

public static void main(String[] args) {
    Main m = new Main();
    Car car = m.new Car("My Car");
    car.setDescription("This is my car.");
    CsvSerializer csvSerializer = new CsvSerializer();
    String csvStr = (String) csvSerializer.serialize(car);
    System.out.println(csvStr);
}

}

但是,当我运行我的实现时,我得到以下输出:

~unique-id~,class,description
0,Main$Car,

我不明白为什么在我的实现中 builddescription 字段都没有被序列化,你能帮忙吗?

干杯,

皮特

I am trying to use SOJO to serialize a Java object to CSV. The example looks pretty straight forward:

Car car = new Car("My Car");
car.setDescription("This is my car.");
Serializer csvSerializer = new CsvSerializer();
String csvStr = (String) csvSerializer.serialize(car);
System.out.println(csvStr);
// print:
// description,build,properties,name,~unique-id~,class  
// This is my car.,,,My Car,0,test.net.sf.sojo.model.Car

I tried implementing my own version of the example. I made a really simple Car class with two String fields (build and description) which implements a setDescription(..) method.

This is what I implemented:

import net.sf.sojo.interchange.csv.CsvSerializer;


public class Main {

private class Car
{
    private String build;
    private String description;
    public Car(String build) {
        this.build = build;
        this.description = null;
    }

    public void setDescription(String description) {
        this.description = description;
    }


}

public static void main(String[] args) {
    Main m = new Main();
    Car car = m.new Car("My Car");
    car.setDescription("This is my car.");
    CsvSerializer csvSerializer = new CsvSerializer();
    String csvStr = (String) csvSerializer.serialize(car);
    System.out.println(csvStr);
}

}

However, when I run my implementation I get the following output:

~unique-id~,class,description
0,Main$Car,

I don't understand why in my implementation neither the build or description fields are serialized, can you help?

Cheers,

Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘落散花 2024-10-22 09:08:35

从 SOJO 主页:“这个项目的目的是一个 Java 框架,以简化的表示形式转换 JavaBeans”

您的示例中的 Car 对象不符合条件。对于您希望 SOJO 写入(或读取)文件的每个属性,您必须有一个 getter(可能还有一个 setter)。添加 getBuild() 和 getDescription()

From the SOJO home page: "The intention for this project is a Java framework, that convert JavaBeans in a simplified representation"

The Car object in your example does not qualify. You must have a getter (and, probabaly, a setter as well) for every property that you wish SOJO to write to (or read from) your file. Add getBuild() and getDescription()

拥抱我好吗 2024-10-22 09:08:35

我没有使用过 SOJO,但是对于私有字段,您可能需要 getter 方法;或者您可以尝试将这些字段声明为公共。

I haven't used SOJO, but for private fields you probably need getter methods; or you could try declaring the fields public.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文