write and read this:
[
{
"id": 1,
"firstname": "Bart",
"lastname": "Simpson",
"address": {
"city": "Springfield",
"country": "USA"
}
},
{
"id": 2,
"firstname": "Homer",
"lastname": "Simpson",
"address": {
"city": "Springfield",
"country": "USA"
}
},
{
"id": 3,
"firstname": "Mickey",
"lastname": "Mouse",
"address": {
"city": "Orlando",
"country": "USA"
}
}
PersonJson:
public class PersonJSON {
private int id;
private String firstname;
private String lastname;
private AddressJSON address;
public PersonJSON(int id, String firstname, String lastname, AddressJSON address)
{
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public AddressJSON getAddress() {
return address;
}
public void setAddress(AddressJSON address) {
this.address = address;
}
public String toString()
{
return "id: " + this.id + " - Firstname: " + this.firstname + " - Lastname: " + this.lastname + ", " + getAddress().toString();
}
}
AddressJson:
import com.google.gson.annotations.SerializedName;
public class AddressJSON {
//@SerializedName("Cidade")
private String city;
private String country;
public AddressJSON(String city, String country)
{
this.city = city;
this.country = country;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString(){
return city + ", " + country;
}
}
Main:
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
public class PDJSON {
private List<PersonJSON> people;
public PDJSON() {
people = new ArrayList<PersonJSON>();
}
public void saveData(String filePath) {
//Gson gson = new Gson();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try(FileWriter fw = new FileWriter(filePath)){
gson.toJson(people, fw);
}catch(Exception e)
{
e.printStackTrace();
}
}
public List<PersonJSON> loadData(String filePath) {
Gson gson = new Gson();
JsonReader jsonReader = null;
final Type CUS_LIST_TYPE = new TypeToken<List<PersonJSON>>() {}.getType();
//or TypeToken.getParameterized(ArrayList.class, PersonJSON.class).getType();
try{
jsonReader = new JsonReader(new FileReader(filePath));
}catch (Exception e) {
e.printStackTrace();
}
return gson.fromJson(jsonReader, CUS_LIST_TYPE);
}
public static void main(String[] args) {
PDJSON pdj = new PDJSON();
pdj.people.add(new PersonJSON(1,"Bart", "Simpson", new AddressJSON("Springfield", "USA")));
pdj.people.add(new PersonJSON(2,"Homer", "Simpson", new AddressJSON("Springfield", "USA")));
pdj.people.add(new PersonJSON(3,"Mickey", "Mouse", new AddressJSON("Orlando", "USA")));
pdj.saveData("resources/listofpeople.json");
List<PersonJSON> lp = pdj.loadData("resources/listofpeople.json");
for(PersonJSON pj : lp)
{
System.out.println(pj.toString());
}
}
}