I have the following class:
public class Person {
private String id;
private String first_name;
private String last_name;
Person(String id, String first_name, String last_name) {
this.id = id;
this.last_name = last_name;
this.first_name = first_name;
}
}
I want to be able to have a file that has JSON format so:
[
{
"id":"1",
"first_name":"bob",
"last_name":"marley"
},
{
"id":"2",
"first_name":"bob",
"last_name":"ross"
}
]
The file is a list of all Person
objects. So I'm trying to create two methods:
- Method that converts
List<Person>
into a JSON file like above. - Method that reads the JSON file into a
List<Person>
.
What would be the best way (most elegant way) to implement this in Java 8? Do I need to add some methods to Person
class? Or is there another way?
Since it's a List of objects that contain only Strings, is there something elegant for that?