Reputation: 312
At this moment I am working on generating csv from object and I got stuck. I dont know how to generate csv for nested class. For example we have got following classes:
@Getter
@Setter
public class Person {
private int age;
private Name name;
}
@Getter
@Setter
public class Name {
private String firstName;
private String lastName;
}
There is huge problem for. Actually I tested few libraries like jackson and commons csv and I did not find solution. The main problem is that I dont know what fields are in object and have to prepare headers dynamically. Is there any simple way or library which allows for that operation? I would be thankfull for any tips.
Upvotes: 1
Views: 6916
Reputation: 45
What is the purpose of creating the csv file? Is it necessary for you to preserve the nesting of classes? Are you re-constructing the objects from the csv files?
If you're only interested in the data of your objects, consider reducing everything down to the primitive types (or whatever class you have a meaningful toString() for), aka flattening your hierarchy. age, name ==> age, firstName, lastName
You can use reflection to dynamically get the hierarchy.
Does the output have to be a csv? Did you consider using a different format that preserves hierarchy like JSON or XML?
Upvotes: 0
Reputation: 170
You might have got the answer if googled it. It already has answer here : Object to CSV
Using super csv : Using super csv to write in csv from object
Upvotes: 1