Last active
April 5, 2016 10:21
-
-
Save wonderb0lt/fa4e90223b3428d261adb0a50b226987 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
public class Main { | |
static String jsonString = "{\n" + | |
"\t\"1002001\": {\n" + | |
"\t\t\"level\": 2,\n" + | |
"\t\t\"name\": \"CaptKrunch\",\n" + | |
"\t\t\"uid\": 1002001,\n" + | |
"\t\t\"user\": {\n" + | |
"\t\t\t\"age\": 21,\n" + | |
"\t\t\t\"chat_color\": \"CC3299\",\n" + | |
"\t\t\t\"city\": \"None\",\n" + | |
"\t\t\t\"country\": \"United States\",\n" + | |
"\t\t\t\"creation\": 1269969663\n" + | |
"\t\t},\n" + | |
"\t\t\"meta\": {\n" + | |
"\t\t\t\"score\": 1762.000,\n" + | |
"\t\t\t\"flags\": 6176,\n" + | |
"\t\t\t\"kbit\": 0,\n" + | |
"\t\t\t\"lastnews\": 1459736973,\n" + | |
"\t\t\t\"rank\": 78\n" + | |
"\t\t}\n" + | |
"\t},\n" + | |
"\t\"1003001\": {\n" + | |
"\t\t\"level\": 11,\n" + | |
"\t\t\"name\": \"LtRaine\",\n" + | |
"\t\t\"uid\": 1003001,\n" + | |
"\t\t\"user\": {\n" + | |
"\t\t\t\"age\": 35,\n" + | |
"\t\t\t\"chat_color\": \"FFAABB\",\n" + | |
"\t\t\t\"city\": \"LA\",\n" + | |
"\t\t\t\"country\": \"United States\",\n" + | |
"\t\t\t\"creation\": 1269369663\n" + | |
"\t\t},\n" + | |
"\t\t\"meta\": {\n" + | |
"\t\t\t\"score\": 11562.000,\n" + | |
"\t\t\t\"flags\": 1176,\n" + | |
"\t\t\t\"kbit\": 2048,\n" + | |
"\t\t\t\"lastnews\": 1279736973,\n" + | |
"\t\t\t\"rank\": 11\n" + | |
"\t\t}\n" + | |
"\t}\n" + | |
"}"; | |
public static void main(String[] args) { | |
Type type = new TypeToken<Map<String, YourPOJO>>() { | |
}.getType(); | |
Map<String, YourPOJO> map = new Gson().fromJson(jsonString, type); | |
System.out.println(map); | |
YourPOJO first = map.get("1002001"); | |
System.out.println(first.getName()); | |
System.out.println(first.getUser().getAge()); | |
} | |
} |
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
public class User { | |
private int age; | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
} |
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
public class YourPOJO { | |
private String name; | |
private int level; | |
private User user; | |
public User getUser() { | |
return user; | |
} | |
public void setUser(User user) { | |
this.user = user; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getLevel() { | |
return level; | |
} | |
public void setLevel(int level) { | |
this.level = level; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment