Skip to content

Instantly share code, notes, and snippets.

@FreeFly19
Created January 30, 2017 17:08
Show Gist options
  • Save FreeFly19/899ef6e6c82cfb961b32a8010e73bbb0 to your computer and use it in GitHub Desktop.
Save FreeFly19/899ef6e6c82cfb961b32a8010e73bbb0 to your computer and use it in GitHub Desktop.
Gson example
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
Human human = new Human();
human.setAge(21);
human.setName("Sasha");
String humanJson = gson.toJson(human);
//serialization, stringify [object -> string]
//deserialization, parse [string -> object]
String json = "{\"name\": \"Ruslan\", \"age\": 15}";
Human human1 = gson.fromJson(json, Human.class);
System.out.println(human1.getName());
System.out.println(human1.getAge());
}
public static class Human {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment