1

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.

2
  • Is there any particular reason you want to use CSV over e.g. JSON? Commented Jul 11, 2016 at 16:06
  • There are some reasons Commented Jul 11, 2016 at 16:55

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Actually it is not working with nested class, only for pojos
Csvs don't work for nested classes. They can be represented in excel format too. Nested classes can't be represented in that format. You have to go with plain pojos.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.