Created
January 30, 2017 17:08
-
-
Save FreeFly19/899ef6e6c82cfb961b32a8010e73bbb0 to your computer and use it in GitHub Desktop.
Gson example
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
| 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