Gson真好用

Gson拥有处理大型复杂json数据的能力,特别是配合GsonFormat的使用,简直无敌。

安装GsonFormat

Android Studio直接安装

File->Settings..->Plugins–>Browse repositores..搜索GsonFormat

Android Studio间接安装

1.下载 GsonFormat.jar

2.Android studio File->Settings..->Plugins –>install plugin from disk..导入下载的 GsonFormat.jar ,

3.重启 android studio.

使用GsonFormat

因为我们是采用Gson来解决数据,Gson可以把json数据直接转成java对象,所以,要先建造一个对应json数据的javaBean对象。
我们先建好javaBean类(自己起名字),然后在Bean类里面右键




这时候,json数据所对应的JavaBean就建好了。

使用Gson

1
2
3
4
5
6
//Gson对象
Gson gson = new Gson();
//将json转为javabean对象 这是json数据
JsonBean jsonBean = gson.fromJson(table_response,JsonBean.class);
//
List<JsonBean.ResultBean.SectionsBean> sectionsBeanList = jsonBean.getResult().getSections();

如果我要获取最深处的数据怎么办呢?

1
sectionsBeanList.get(0).getCourses().get(1).getName();

这样就得到了json最深处的数据=朱慧丽

附上一段json数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{
"result": {
"ShowSaturday": true,
"ShowSunday": false,
"ShowTeacher": true,
"Title": "2017-2018学年第一学期课表 (单周)",
"SubTitle": null,
"StartDate": "2017年09月01日",
"EndDate": "2019年02月10日",
"Sections": [
{
"Name": "第一节",
"Index": 2,
"Courses": [
{
"Name": "升旗",
"Week": 1,
"Teacher": "马丽娟",
"Show": true,
"TypeID": 5,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "英语",
"Week": 2,
"Teacher": "朱慧丽",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "英语",
"Week": 3,
"Teacher": "朱慧丽",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "化学",
"Week": 4,
"Teacher": "胡云仙",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "历史",
"Week": 5,
"Teacher": "周靖娴",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "-",
"Week": 6,
"Teacher": null,
"Show": true,
"TypeID": 0,
"ClassRoom": null,
"FontSize": 20,
"TeacherFontSize": 12,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "-",
"Week": 0,
"Teacher": null,
"Show": false,
"TypeID": 0,
"ClassRoom": null,
"FontSize": 20,
"TeacherFontSize": 12,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
}
],
"StartTime": "07:40",
"EndTime": "08:20"
},
{
"Name": "第二节",
"Index": 3,
"Courses": [
{
"Name": "数学",
"Week": 1,
"Teacher": "曹武庆",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "物理",
"Week": 2,
"Teacher": "周宇",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "语文",
"Week": 3,
"Teacher": "张茫茫",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "语文",
"Week": 4,
"Teacher": "张茫茫",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "语文",
"Week": 5,
"Teacher": "张茫茫",
"Show": true,
"TypeID": 1,
"ClassRoom": null,
"FontSize": 18,
"TeacherFontSize": 14,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "-",
"Week": 6,
"Teacher": null,
"Show": true,
"TypeID": 0,
"ClassRoom": null,
"FontSize": 20,
"TeacherFontSize": 12,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
},
{
"Name": "-",
"Week": 0,
"Teacher": null,
"Show": false,
"TypeID": 0,
"ClassRoom": null,
"FontSize": 20,
"TeacherFontSize": 12,
"StartTime": "00:00:00",
"EndTime": "00:00:00",
"KQStartTime": "00:00:00",
"KQEndTime": "00:00:00"
}
],
"StartTime": "08:30",
"EndTime": "09:10"
}
]