Java SOJO CSV 序列化
我正在尝试使用 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
字段(build
和 description
),它实现了 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,
我不明白为什么在我的实现中 build
或 description
字段都没有被序列化,你能帮忙吗?
干杯,
皮特
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 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()
我没有使用过 SOJO,但是对于私有字段,您可能需要 getter 方法;或者您可以尝试将这些字段声明为公共。
I haven't used SOJO, but for private fields you probably need getter methods; or you could try declaring the fields public.