Created
March 24, 2015 20:15
-
-
Save joanmolinas/933fca2942da97f007a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//For use Gson, you need a Gson jar. Download from this https://code.google.com/p/google-gson/ | |
class Student { | |
private String name; | |
private int mark; | |
private String city; | |
public Student(String name, int mark, String city) { | |
this.name = name; | |
this.mark = mark; | |
this.city = city; | |
} | |
@Override | |
public String toString() { | |
return "Student [name=" + name + ", mark=" + mark + "]"; | |
} | |
} | |
class Group { | |
private ArrayList<Student> students; | |
public Group() { | |
students = new ArrayList<Student>(); | |
} | |
public void add(Student s){ | |
students.add(s); | |
} | |
@Override | |
public String toString() { | |
return "Group [students=" + students + "]"; | |
} | |
} | |
//Use this for create json format or read json format | |
String json; | |
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | |
Group g = new Group(); | |
g.add(new Student("John", 10, "Barcelona")); | |
g.add(new Student("Daniel", 5, "Madrid")); | |
json = gson.toJson(g); | |
System.out.println(json); | |
Group g2 = gson.fromJson(json, Group.class); | |
System.out.println(g2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment