Gson 的基本使用
1 import java.util.ArrayList;
2 import java.util.HashMap;
3 import java.util.Iterator;
4 import java.util.List;
5
6 import com.google.gson.Gson;
7 import com.google.gson.GsonBuilder;
8 import com.google.gson.reflect.TypeToken;
9
10 class User {
11 private String username;
12 private String password;
13
14 public String getUsername() {
15 return username;
16 }
17
18 public void setUsername(String username) {
19 this.username = username;
20 }
21
22 public String getPassword() {
23 return password;
24 }
25
26 public void setPassword(String password) {
27 this.password = password;
28 }
29
30 @Override
31 public String toString() {
32 return username + "'s password is:" + password;
33 }
34 }
35
36 class UserGroup {
37 private String id;
38 private HashMap<String, User> usermap;
39 private List<User> userlist;
40
41 public String getId() {
42 return id;
43 }
44
45 public void setId(String id) {
46 this.id = id;
47 }
48
49 public HashMap<String, User> getUsermap() {
50 return usermap;
51 }
52
53 public void setUsermap(HashMap<String, User> usermap) {
54 this.usermap = usermap;
55 }
56
57 public List<User> getUserlist() {
58 return userlist;
59 }
60
61 public void setUserlist(List<User> userlist) {
62 this.userlist = userlist;
63 }
64
65 public String toString() {
66 String str = "";
67 Iterator it = usermap.keySet().iterator();
68 while (it.hasNext()) {
69 Object key = it.next();
70 str+="key:" + key+" , value:" + usermap.get(key)+"\n";
71 }
72
73 return "id:" + id + "\n"+str;
74 }
75 }
76
77 public class testJson {
78
79 /**
80 * @param args
81 */
82 public static void main(String[] args) {
83 BeanGroupDemo();
84
85 }
86
87 static void BeanDemo() {
88 User user = new User();
89 user.setUsername("zhoubiwen");
90 user.setPassword("123456");
91
92 GsonBuilder builder = new GsonBuilder();
93 // builder.excludeFieldsWithoutExposeAnnotation();
94 Gson gson = builder.create();
95 String jsonUser = gson.toJson(user);
96 System.out.println(jsonUser);
97
98 User user2 = new User();
99 user2 = gson.fromJson(jsonUser, User.class);
100 System.out.println(user2);
101 }
102
103 static void BeanGroupDemo() {
104 UserGroup users = new UserGroup();
105 users.setId("001");
106 HashMap<String, User> usermap = new HashMap<String, User>();
107 User user1 = new User();
108 user1.setUsername("001");
109 user1.setPassword("001pwd");
110 User user2 = new User();
111 user2.setUsername("002");
112 user2.setPassword("002pwd");
113 usermap.put("user1", user1);
114 usermap.put("user2", user2);
115
116 users.setUsermap(usermap);
117
118 GsonBuilder builder = new GsonBuilder();
119 Gson gson = builder.create();
120 String jsonUser = gson.toJson(users);
121 System.out.println(jsonUser);
122
123 UserGroup users2 = new UserGroup();
124 users2 = gson.fromJson(jsonUser, UserGroup.class);
125 System.out.println(users2);
126 }
127
128 static void ListDemo() {
129 Gson gson = new Gson();
130 List<String> list = new ArrayList<String>();
131 list.add("one");
132 list.add("two");
133 list.add("three");
134 String gsonstr = gson.toJson(list);
135 System.out.println(gsonstr);
136 list.clear();
137 list = gson.fromJson(gsonstr, new TypeToken<ArrayList<String>>() {
138 }.getType());
139 System.out.println(list);
140 }
141
142 static void HashMapDemo() {
143 Gson gson = new Gson();
144 HashMap map = new HashMap();
145 map.put("k1", "v1");
146 map.put("k2", "v2");
147 map.put("k3", "v3");
148 String gsonstr = gson.toJson(map);
149 System.out.println(gsonstr);
150 map.clear();
151 map = gson.fromJson(gsonstr, new TypeToken<HashMap<String, String>>() {
152 }.getType());
153 System.out.println(map);
154
155 }
156
157 }
158
2 import java.util.HashMap;
3 import java.util.Iterator;
4 import java.util.List;
5
6 import com.google.gson.Gson;
7 import com.google.gson.GsonBuilder;
8 import com.google.gson.reflect.TypeToken;
9
10 class User {
11 private String username;
12 private String password;
13
14 public String getUsername() {
15 return username;
16 }
17
18 public void setUsername(String username) {
19 this.username = username;
20 }
21
22 public String getPassword() {
23 return password;
24 }
25
26 public void setPassword(String password) {
27 this.password = password;
28 }
29
30 @Override
31 public String toString() {
32 return username + "'s password is:" + password;
33 }
34 }
35
36 class UserGroup {
37 private String id;
38 private HashMap<String, User> usermap;
39 private List<User> userlist;
40
41 public String getId() {
42 return id;
43 }
44
45 public void setId(String id) {
46 this.id = id;
47 }
48
49 public HashMap<String, User> getUsermap() {
50 return usermap;
51 }
52
53 public void setUsermap(HashMap<String, User> usermap) {
54 this.usermap = usermap;
55 }
56
57 public List<User> getUserlist() {
58 return userlist;
59 }
60
61 public void setUserlist(List<User> userlist) {
62 this.userlist = userlist;
63 }
64
65 public String toString() {
66 String str = "";
67 Iterator it = usermap.keySet().iterator();
68 while (it.hasNext()) {
69 Object key = it.next();
70 str+="key:" + key+" , value:" + usermap.get(key)+"\n";
71 }
72
73 return "id:" + id + "\n"+str;
74 }
75 }
76
77 public class testJson {
78
79 /**
80 * @param args
81 */
82 public static void main(String[] args) {
83 BeanGroupDemo();
84
85 }
86
87 static void BeanDemo() {
88 User user = new User();
89 user.setUsername("zhoubiwen");
90 user.setPassword("123456");
91
92 GsonBuilder builder = new GsonBuilder();
93 // builder.excludeFieldsWithoutExposeAnnotation();
94 Gson gson = builder.create();
95 String jsonUser = gson.toJson(user);
96 System.out.println(jsonUser);
97
98 User user2 = new User();
99 user2 = gson.fromJson(jsonUser, User.class);
100 System.out.println(user2);
101 }
102
103 static void BeanGroupDemo() {
104 UserGroup users = new UserGroup();
105 users.setId("001");
106 HashMap<String, User> usermap = new HashMap<String, User>();
107 User user1 = new User();
108 user1.setUsername("001");
109 user1.setPassword("001pwd");
110 User user2 = new User();
111 user2.setUsername("002");
112 user2.setPassword("002pwd");
113 usermap.put("user1", user1);
114 usermap.put("user2", user2);
115
116 users.setUsermap(usermap);
117
118 GsonBuilder builder = new GsonBuilder();
119 Gson gson = builder.create();
120 String jsonUser = gson.toJson(users);
121 System.out.println(jsonUser);
122
123 UserGroup users2 = new UserGroup();
124 users2 = gson.fromJson(jsonUser, UserGroup.class);
125 System.out.println(users2);
126 }
127
128 static void ListDemo() {
129 Gson gson = new Gson();
130 List<String> list = new ArrayList<String>();
131 list.add("one");
132 list.add("two");
133 list.add("three");
134 String gsonstr = gson.toJson(list);
135 System.out.println(gsonstr);
136 list.clear();
137 list = gson.fromJson(gsonstr, new TypeToken<ArrayList<String>>() {
138 }.getType());
139 System.out.println(list);
140 }
141
142 static void HashMapDemo() {
143 Gson gson = new Gson();
144 HashMap map = new HashMap();
145 map.put("k1", "v1");
146 map.put("k2", "v2");
147 map.put("k3", "v3");
148 String gsonstr = gson.toJson(map);
149 System.out.println(gsonstr);
150 map.clear();
151 map = gson.fromJson(gsonstr, new TypeToken<HashMap<String, String>>() {
152 }.getType());
153 System.out.println(map);
154
155 }
156
157 }
158