一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|

服務器之家 - 編程語言 - JAVA教程 - Java中利用gson解析Json實例教程

Java中利用gson解析Json實例教程

2020-10-27 16:38jihite JAVA教程

這篇文章主要給大家介紹了關于Java中利用gson解析Json 的相關資料,文中給出了詳細的示例代碼供大家參考學習,相信對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

本文主要跟大家介紹了關于Java用gson解析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
224
225
{
 
 "resultcode": "200",
 
 "reason": "successed!",
 
 "result": {
 
  "sk": {
 
   "temp": "24",
 
   "wind_direction": "西南風",
 
   "wind_strength": "2級",
 
   "humidity": "51%",
 
   "time": "10:11"
 
  },
 
  "today": {
 
   "temperature": "16℃~27℃",
 
   "weather": "陰轉多云",
 
   "weather_id": {
 
    "fa": "02",
 
    "fb": "01"
 
   },
 
   "wind": "西南風3-4 級",
 
   "week": "星期四",
 
   "city": "濱州",
 
   "date_y": "2015年06月04日",
 
   "dressing_index": "舒適",
 
   "dressing_advice": "建議著長袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。",
 
   "uv_index": "最弱",
 
   "comfort_index": "",
 
   "wash_index": "較適宜",
 
   "travel_index": "",
 
   "exercise_index": "較適宜",
 
   "drying_index": ""
 
  },
 
  "future": [
 
   {
 
    "temperature": "16℃~27℃",
 
    "weather": "陰轉多云",
 
    "weather_id": {
 
     "fa": "02",
 
     "fb": "01"
 
    },
 
    "wind": "西南風3-4 級",
 
    "week": "星期四",
 
    "date": "20150604"
 
   },
 
   {
 
    "temperature": "20℃~32℃",
 
    "weather": "多云轉晴",
 
    "weather_id": {
 
     "fa": "01",
 
     "fb": "00"
 
    },
 
    "wind": "西風3-4 級",
 
    "week": "星期五",
 
    "date": "20150605"
 
   },
 
   {
 
    "temperature": "23℃~35℃",
 
    "weather": "多云轉陰",
 
    "weather_id": {
 
     "fa": "01",
 
     "fb": "02"
 
    },
 
    "wind": "西南風3-4 級",
 
    "week": "星期六",
 
    "date": "20150606"
 
   },
 
   {
 
    "temperature": "20℃~33℃",
 
    "weather": "多云",
 
    "weather_id": {
 
     "fa": "01",
 
     "fb": "01"
 
    },
 
    "wind": "北風微風",
 
    "week": "星期日",
 
    "date": "20150607"
 
   },
 
   {
 
    "temperature": "22℃~34℃",
 
    "weather": "多云",
 
    "weather_id": {
 
     "fa": "01",
 
     "fb": "01"
 
    },
 
    "wind": "西南風3-4 級",
 
    "week": "星期一",
 
    "date": "20150608"
 
   },
 
   {
 
    "temperature": "22℃~33℃",
 
    "weather": "陰",
 
    "weather_id": {
 
     "fa": "02",
 
     "fb": "02"
 
    },
 
    "wind": "西南風3-4 級",
 
    "week": "星期二",
 
    "date": "20150609"
 
   },
 
   {
 
    "temperature": "22℃~33℃",
 
    "weather": "多云",
 
    "weather_id": {
 
     "fa": "01",
 
     "fb": "01"
 
    },
 
    "wind": "南風3-4 級",
 
    "week": "星期三",
 
    "date": "20150610"
 
   }
 
  ]
 
 },
 
 "error_code": 0
 
}

解析JSONObject

?
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
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException;
 
import java.io.FileNotFoundException;
import java.io.FileReader;
 
public class ReadJson {
 public static void main(String []args) {
  JsonParser parse = new JsonParser();
  try {
   JsonObject json = (JsonObject) parse.parse(new FileReader("weather.json"));
   System.out.println("resultcode:" + json.get("resultcodeu").getAsInt());
   System.out.println("reason:" + json.get("reason").getAsString());
   JsonObject result = json.get("result").getAsJsonObject();
   JsonObject today = result.get("today").getAsJsonObject();
   System.out.println("weak:" + today.get("week").getAsString());
   System.out.println("weather:" + today.get("weather").getAsString());
  } catch (JsonIOException e) {
   e.printStackTrace();
  } catch (NullPointerException e) {
   e.printStackTrace();
  } catch (JsonSyntaxException e){
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
}

解析JSONArray

?
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
import com.google.gson.JsonParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.JsonIOException;
 
import java.io.FileNotFoundException;
import java.io.FileReader;
 
public class ReadJsonArray {
 public static void main(String []args) {
  JsonParser parse = new JsonParser();
  try {
   JsonObject json = (JsonObject)parse.parse(new FileReader("C:\\Users\\wzh94434\\IdeaProjects\\TestProject\\jsontest\\src\\main\\java\\weather.json"));
   JsonObject result = json.get("result").getAsJsonObject();
   JsonArray futureArray = result.get("future").getAsJsonArray();
   for (int i = 0; i < futureArray.size(); ++i) {
    JsonObject subObj = futureArray.get(i).getAsJsonObject();
    System.out.println("------");
    System.out.println("week:" + subObj.get("week").getAsString());
    System.out.println("weather:" + subObj.get("weather").getAsString());
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (JsonIOException e) {
   e.printStackTrace();
  } catch (JsonSyntaxException e) {
   e.printStackTrace();
  }
 }
}

注意:文件路徑相對路徑是從工程根目錄開始

Java中利用gson解析Json實例教程

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.cnblogs.com/kaituorensheng/p/6616126.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天草视频 | 国产日韩精品欧美一区 | 日韩毛片基地一区二区三区 | 国产精品久久久久久福利 | 香蕉动漫库 | 成人毛片1024你懂的 | 日女人免费视频 | 色老板在线免费观看 | 亚洲首页国产精品丝袜 | 美女班主任下面好爽好湿好紧 | 猛h辣h高h文湿重口 门房秦大爷在线阅读 | 亚洲va在线va天堂va偷拍 | 外国a级片 | 精品视频久久久久 | 国产成人 免费观看 | 青草视频免费观看在线观看 | 国模大胆一区二区三区 | 激情视频在线播放 | 高清欧美不卡一区二区三区 | 国产自在线观看 | 国产精品久久久久久久久久久搜索 | 青青视频国产依人在线 | 色综合久久中文字幕综合网 | 狠狠色婷婷| 久热这里在线精品 | 日韩精品一区二区三区免费视频 | 高清欧美不卡一区二区三区 | 好妈妈7在线观看高清 | 青草园网站在线观看 | 12345国产精品高清在线 | 男男互操文 | 久久久免费热线精品频 | 国产精品嫩草影院一二三区 | 千金肉奴隶免费观看 | 久久视频这里只精品99热在线观看 | 嫩草影院永久入口在线观看 | 18日本xxxxxxⅹxx96 | 免费观看在线观看 | 午夜伦伦电影理论片费看 | 亚洲香蕉网久久综合影院3p | 性欧美xxxxx老太婆 |