# Android筆記--使用JSON.org類別庫解析&生成JSON數據
**==相關閱讀==**:[物件導向語言筆記--JSON](https://hackmd.io/@9YAtszqXS2OLNZOrLY_-Jg/OOP_JSON)、[Android筆記–使用GSON.org類別庫解析&生成JSON數據](https://hackmd.io/@9YAtszqXS2OLNZOrLY_-Jg/android_GSON)
JSON 是一種常見的數據交換格式,用於在應用程式之間傳遞數據。json.org 則是 JSON 解析的第三方庫之一,它被廣泛應用於 Android 開發中,功能全面且易於使用和學習,但是它也存在一些缺點,例如缺乏現代化特性和官方支持。
:::warning
- **json.org的優點**:
- 功能全面: 提供了解析和生成 JSON 字符串的完整功能。
- 性能良好: 解析和生成 JSON 字符串的效率較高。
- **json.org的缺點**:
- 缺乏現代化特性: 與其他 JSON 解析庫相比,==缺乏一些現代化特性,例如流解析和數據綁定==。
- ==缺乏官方支持==: 沒有來自 Google 或 Android 官方的支持。
:::
### **如果需要更現代化的 JSON 解析庫,可以使用以下替代方案**:
- [Gson](https://hackmd.io/@9YAtszqXS2OLNZOrLY_-Jg/android_GSON): 由 Google 開發的 JSON 解析庫,提供了流解析、數據綁定等現代化特性。
- Moshi: 由 Square 開發的 JSON 解析庫,提供了流解析、數據綁定等現代化特性,並且支持 Kotlin
---
## ▼JSON範例(有夠長,所以附上範例底部的[傳送門](#▼前置:)):
:::success
:::spoiler ※=====本篇採用的JSON範例(備註:源自[中央氣象署開放資料平臺之資料擷取API](https://opendata.cwa.gov.tw/dist/opendata-swagger.html#/%E5%9C%B0%E9%9C%87%E6%B5%B7%E5%98%AF/get_v1_rest_datastore_E_A0014_001))=====※
```
{
"success": "true",
"result": {
"resource_id": "E-A0014-001",
"fields": [
{
"id": "ReportType",
"type": "String"
},
{
"id": "ReportNo",
"type": "String"
},
{
"id": "ReportContent",
"type": "String"
},
{
"id": "ReportColor",
"type": "String"
},
{
"id": "TsunamiNo",
"type": "Integer"
},
{
"id": "Web",
"type": "String"
},
{
"id": "OriginTime",
"type": "Timestamp"
},
{
"id": "EpicenterLatitude",
"type": "Float"
},
{
"id": "EpicenterLongitude",
"type": "Float"
},
{
"id": "Location",
"type": "String"
},
{
"id": "FocalDepth",
"type": "Float"
},
{
"id": "unit",
"type": "String"
},
{
"id": "MagnitudeValue",
"type": "Float"
},
{
"id": "Source",
"type": "String"
},
{
"id": "AreaDesc",
"type": "String"
},
{
"id": "AreaName",
"type": "String"
},
{
"id": "InfoStatus",
"type": "String"
},
{
"id": "WaveHeight",
"type": "String"
},
{
"id": "ArrivalTime",
"type": "Timestamp"
},
{
"id": "AreaColor",
"type": "String"
},
{
"id": "StationName",
"type": "String"
},
{
"id": "StationID",
"type": "String"
},
{
"id": "IntScaleValue",
"type": "Float"
},
{
"id": "IntScaleValue",
"type": "Float"
}
]
},
"records": {
"datasetDescription": "海嘯資訊",
"Tsunami": [
{
"ReportColor": "綠色",
"ReportContent": "根據中央氣象署潮位站觀測,海嘯波浪已經陸續抵達沿岸,經研判海嘯的威脅解除,故解除海嘯警報。",
"ReportNo": "第3報",
"ReportType": "海嘯警報解除",
"TsunamiNo": 113003,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11300303",
"EarthquakeInfo": {
"OriginTime": "2024-04-03 07:58:00",
"Source": "中央氣象署",
"FocalDepth": 15.5,
"Epicenter": {
"Location": "臺灣",
"EpicenterLatitude": 23.77,
"EpicenterLongitude": 121.67
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.2
}
},
"TsunamiWave": {
"WarningArea": [],
"TsuStation": [
{
"ArrivalTime": "2024-04-03 08:30:00",
"InfoStatus": "observe",
"StationID": "HL",
"StationName": "花蓮",
"StationLatitude": 23.98,
"StationLongitude": 121.62,
"WaveHeight": "27公分"
},
{
"ArrivalTime": "2024-04-03 08:33:00",
"InfoStatus": "observe",
"StationID": "SA",
"StationName": "宜蘭蘇澳",
"StationLatitude": 24.59,
"StationLongitude": 121.87,
"WaveHeight": "43公分"
},
{
"ArrivalTime": "2024-04-03 08:35:00",
"InfoStatus": "observe",
"StationID": "LD",
"StationName": "新北龍洞",
"StationLatitude": 25.1,
"StationLongitude": 121.92,
"WaveHeight": "22公分"
},
{
"ArrivalTime": "2024-04-03 08:49:00",
"InfoStatus": "observe",
"StationID": "WK",
"StationName": "嘉義塭港",
"StationLatitude": 23.47,
"StationLongitude": 120.12,
"WaveHeight": "21公分"
},
{
"ArrivalTime": "2024-04-03 08:52:00",
"InfoStatus": "observe",
"StationID": "CK",
"StationName": "台東成功",
"StationLatitude": 23.09,
"StationLongitude": 121.38,
"WaveHeight": "54公分"
},
{
"ArrivalTime": "2024-04-03 09:12:00",
"InfoStatus": "observe",
"StationID": "TO",
"StationName": "宜蘭烏石",
"StationLatitude": 24.87,
"StationLongitude": 121.84,
"WaveHeight": "82公分"
},
{
"ArrivalTime": "2024-04-03 09:36:00",
"InfoStatus": "observe",
"StationID": "臺中港臺中港",
"StationName": "臺中港臺中港",
"StationLatitude": 0,
"StationLongitude": 0,
"WaveHeight": "27公分"
},
{
"ArrivalTime": "2024-04-03 10:30:00",
"InfoStatus": "observe",
"StationID": "LS",
"StationName": "新北麟山鼻",
"StationLatitude": 25.28,
"StationLongitude": 121.51,
"WaveHeight": "26公分"
},
{
"ArrivalTime": "2024-04-03 10:48:00",
"InfoStatus": "observe",
"StationID": "DS",
"StationName": "嘉義東石",
"StationLatitude": 23.44,
"StationLongitude": 120.14,
"WaveHeight": "21公分"
}
]
}
},
{
"ReportColor": "綠色",
"ReportContent": "2024年04月03日07時58分(臺灣時間),臺灣發生規模7﹒2地震,震央位於東經121﹒67度、北緯23﹒77度。該地震引發海嘯已陸續抵達臺灣沿岸,提醒沿海地區民眾提高警覺嚴加防範,注意海浪突然湧升所造成的危害。",
"ReportNo": "第2報",
"ReportType": "海嘯警報",
"TsunamiNo": 113003,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11300302",
"EarthquakeInfo": {
"OriginTime": "2024-04-03 07:58:00",
"Source": "中央氣象署",
"FocalDepth": 15.5,
"Epicenter": {
"Location": "臺灣",
"EpicenterLatitude": 23.77,
"EpicenterLongitude": 121.67
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.2
}
},
"TsunamiWave": {
"WarningArea": [],
"TsuStation": [
{
"ArrivalTime": "2024-04-03 08:30:00",
"InfoStatus": "observe",
"StationID": "HL",
"StationName": "花蓮",
"StationLatitude": 23.98,
"StationLongitude": 121.62,
"WaveHeight": "27公分"
},
{
"ArrivalTime": "2024-04-03 08:33:00",
"InfoStatus": "observe",
"StationID": "SA",
"StationName": "宜蘭蘇澳",
"StationLatitude": 24.59,
"StationLongitude": 121.87,
"WaveHeight": "43公分"
},
{
"ArrivalTime": "2024-04-03 08:35:00",
"InfoStatus": "observe",
"StationID": "LD",
"StationName": "新北龍洞",
"StationLatitude": 25.1,
"StationLongitude": 121.92,
"WaveHeight": "22公分"
},
{
"ArrivalTime": "2024-04-03 08:49:00",
"InfoStatus": "observe",
"StationID": "WK",
"StationName": "嘉義塭港",
"StationLatitude": 23.47,
"StationLongitude": 120.12,
"WaveHeight": "21公分"
},
{
"ArrivalTime": "2024-04-03 08:52:00",
"InfoStatus": "observe",
"StationID": "CK",
"StationName": "台東成功",
"StationLatitude": 23.09,
"StationLongitude": 121.38,
"WaveHeight": "54公分"
},
{
"ArrivalTime": "2024-04-03 09:12:00",
"InfoStatus": "observe",
"StationID": "TO",
"StationName": "宜蘭烏石",
"StationLatitude": 24.87,
"StationLongitude": 121.84,
"WaveHeight": "82公分"
},
{
"ArrivalTime": "2024-04-03 09:36:00",
"InfoStatus": "observe",
"StationID": "臺中港臺中港",
"StationName": "臺中港臺中港",
"StationLatitude": 0,
"StationLongitude": 0,
"WaveHeight": "27公分"
}
]
}
},
{
"ReportColor": "黃色",
"ReportContent": "113年04月03日07時58分(臺灣時間),臺灣東部海域發生規模7﹒3地震,震央位於東經121﹒67度、北緯23﹒77度。該地震可能引發海嘯影響臺灣,特此發布海嘯警報,提醒沿海地區民眾提高警覺嚴加防範,注意海浪突然湧升所造成的危害。",
"ReportNo": "第1報",
"ReportType": "海嘯警報",
"TsunamiNo": 113003,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11300301",
"EarthquakeInfo": {
"OriginTime": "2024-04-03 07:58:00",
"Source": "中央氣象署",
"FocalDepth": 15.5,
"Epicenter": {
"Location": "臺灣東部海域",
"EpicenterLatitude": 23.77,
"EpicenterLongitude": 121.67
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.3
}
},
"TsunamiWave": {
"WarningArea": [
{
"AreaColor": "黃色",
"AreaDesc": "宜蘭縣南澳鄉至臺東縣長濱鄉沿岸",
"AreaName": "東部沿海地區",
"ArrivalTime": "2024-04-03 07:59:00",
"InfoStatus": "predict",
"WaveHeight": "小於1公尺"
},
{
"AreaColor": "黃色",
"AreaDesc": "臺東縣成功鎮至屏東縣滿州鄉沿岸",
"AreaName": "東南沿海地區",
"ArrivalTime": "2024-04-03 08:10:00",
"InfoStatus": "predict",
"WaveHeight": "小於1公尺"
},
{
"AreaColor": "黃色",
"AreaDesc": "宜蘭縣頭城鎮至蘇澳鎮沿岸",
"AreaName": "東北沿海地區",
"ArrivalTime": "2024-04-03 08:13:00",
"InfoStatus": "predict",
"WaveHeight": "小於1公尺"
},
{
"AreaColor": "黃色",
"AreaDesc": "新北市及基隆市沿岸",
"AreaName": "北部沿海地區",
"ArrivalTime": "2024-04-03 08:39:00",
"InfoStatus": "predict",
"WaveHeight": "小於1公尺"
},
{
"AreaColor": "黃色",
"AreaDesc": "臺南市至屏東縣恆春鎮沿岸",
"AreaName": "西南沿海地區",
"ArrivalTime": "2024-04-03 08:54:00",
"InfoStatus": "predict",
"WaveHeight": "小於1公尺"
},
{
"AreaColor": "黃色",
"AreaDesc": "桃園市至嘉義縣沿岸,以及澎湖縣、金門縣與連江縣等離島區域",
"AreaName": "海峽沿海地區",
"ArrivalTime": "2024-04-03 09:58:00",
"InfoStatus": "predict",
"WaveHeight": "小於1公尺"
}
],
"TsuStation": []
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對01月01日15時10分(臺灣時間)日本 靠近本州西海岸規模7﹒4地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 113002,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11300202",
"EarthquakeInfo": {
"OriginTime": "2024-01-01 15:10:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "日本 靠近本州西海岸",
"EpicenterLatitude": 37.5,
"EpicenterLongitude": 137.2
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.4
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2024年01月01日15時10分(臺灣時間),日本 靠近本州西海岸發生規模7﹒4地震,震央位於東經137﹒20度、北緯37﹒50度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 113001,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11300101",
"EarthquakeInfo": {
"OriginTime": "2024-01-01 15:10:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "日本 靠近本州西海岸",
"EpicenterLatitude": 37.5,
"EpicenterLongitude": 137.2
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.4
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對12月07日20時57分(臺灣時間)大洋洲 萬那杜群島規模7﹒1地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 112008,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200802",
"EarthquakeInfo": {
"OriginTime": "2023-12-07 20:57:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 35,
"Epicenter": {
"Location": "大洋洲 萬那杜群島",
"EpicenterLatitude": -20.7,
"EpicenterLongitude": 169.2
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.1
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年12月07日20時57分(臺灣時間),大洋洲 萬那杜群島發生規模7﹒3地震,震央位於東經169﹒20度、南緯20﹒70度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112008,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200801",
"EarthquakeInfo": {
"OriginTime": "2023-12-07 20:57:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 35,
"Epicenter": {
"Location": "大洋洲 萬那杜群島",
"EpicenterLatitude": -20.7,
"EpicenterLongitude": 169.2
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.3
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "根據中央氣象署潮位站觀測,研判海嘯的威脅解除,故解除海嘯警報。",
"ReportNo": "第4報",
"ReportType": "海嘯警報解除",
"TsunamiNo": 112007,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200704",
"EarthquakeInfo": {
"OriginTime": "2023-12-02 22:37:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "菲律賓民答那峨島",
"EpicenterLatitude": 8.6,
"EpicenterLongitude": 126.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.6
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對12月02日22時37分(臺灣時間)菲律賓民答那峨島規模7﹒6地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第3報",
"ReportType": "海嘯消息",
"TsunamiNo": 112007,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200703",
"EarthquakeInfo": {
"OriginTime": "2023-12-02 22:37:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "菲律賓民答那峨島",
"EpicenterLatitude": 8.6,
"EpicenterLongitude": 126.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.6
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年12月02日22時37分(臺灣時間),菲律賓民答那峨島發生規模7﹒6地震,震央位於東經126﹒40度、北緯8﹒60度。經本署研判,該地震所引發海嘯預計約於12月03日00時46分開始影響臺灣,預估潮位低於0﹒3公尺,特此發布海嘯警報,提醒沿海地區民眾提高警覺嚴加防範,注意海浪突然湧升所造成的危害。",
"ReportNo": "第2報",
"ReportType": "海嘯警報",
"TsunamiNo": 112007,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200702",
"EarthquakeInfo": {
"OriginTime": "2023-12-02 22:37:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "菲律賓民答那峨島",
"EpicenterLatitude": 8.6,
"EpicenterLongitude": 126.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.6
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年12月02日22時37分(臺灣時間),菲律賓民答那峨島發生規模7﹒7地震,震央位於東經126﹒40度、北緯8﹒60度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112007,
"Web": "https://scweb.cwa.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200701",
"EarthquakeInfo": {
"OriginTime": "2023-12-02 22:37:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "菲律賓民答那峨島",
"EpicenterLatitude": 8.6,
"EpicenterLongitude": 126.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.7
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對05月20日09時51分(臺灣時間)大洋洲 羅亞爾特群島東南方規模7﹒1地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第3報",
"ReportType": "海嘯消息",
"TsunamiNo": 112006,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200603",
"EarthquakeInfo": {
"OriginTime": "2023-05-20 09:51:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.1,
"EpicenterLongitude": 170.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.1
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年05月20日09時51分(臺灣時間),大洋洲 羅亞爾特群島東南方發生規模7﹒1地震,震央位於東經170﹒40度、南緯23﹒10度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 112006,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200602",
"EarthquakeInfo": {
"OriginTime": "2023-05-20 09:51:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.1,
"EpicenterLongitude": 170.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.1
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年05月20日09時51分(臺灣時間),大洋洲 羅亞爾特群島東南方發生規模7﹒4地震,震央位於東經170﹒40度、南緯23﹒10度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112006,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200601",
"EarthquakeInfo": {
"OriginTime": "2023-05-20 09:51:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 45,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.1,
"EpicenterLongitude": 170.4
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.4
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對05月19日10時57分(臺灣時間)大洋洲 羅亞爾特群島東南方規模7﹒7地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第4報",
"ReportType": "海嘯消息",
"TsunamiNo": 112005,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200504",
"EarthquakeInfo": {
"OriginTime": "2023-05-19 10:57:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.2,
"EpicenterLongitude": 170.7
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.7
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年05月19日10時57分(臺灣時間),大洋洲 羅亞爾特群島東南方發生規模7﹒7地震,震央位於東經170﹒70度、南緯23﹒20度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第3報",
"ReportType": "海嘯消息",
"TsunamiNo": 112005,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200503",
"EarthquakeInfo": {
"OriginTime": "2023-05-19 10:57:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.2,
"EpicenterLongitude": 170.7
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.7
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年05月19日10時57分(臺灣時間),大洋洲 羅亞爾特群島東南方發生規模7﹒8地震,震央位於東經170﹒70度、南緯23﹒20度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 112005,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200502",
"EarthquakeInfo": {
"OriginTime": "2023-05-19 10:57:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.2,
"EpicenterLongitude": 170.7
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.8
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年05月19日10時57分(臺灣時間),大洋洲 羅亞爾特群島東南方發生規模7﹒7地震,震央位於東經170﹒70度、南緯23﹒20度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112005,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200501",
"EarthquakeInfo": {
"OriginTime": "2023-05-19 10:57:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "大洋洲 羅亞爾特群島東南方",
"EpicenterLatitude": -23.2,
"EpicenterLongitude": 170.7
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.7
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對04月24日08時42分(臺灣時間)南太平洋 克馬德克群島規模7﹒1地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 112004,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200402",
"EarthquakeInfo": {
"OriginTime": "2023-04-24 08:42:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "南太平洋 克馬德克群島",
"EpicenterLatitude": -30.4,
"EpicenterLongitude": 183.3
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.1
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年04月24日08時42分(臺灣時間),南太平洋 克馬德克群島發生規模7﹒3地震,震央位於西經176﹒70度、南緯30﹒40度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112004,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200401",
"EarthquakeInfo": {
"OriginTime": "2023-04-24 08:42:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "南太平洋 克馬德克群島",
"EpicenterLatitude": -30.4,
"EpicenterLongitude": 183.3
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.3
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對03月16日08時56分(臺灣時間)南太平洋 克馬德克群島規模7﹒0地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 112003,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200302",
"EarthquakeInfo": {
"OriginTime": "2023-03-16 08:56:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "南太平洋 克馬德克群島",
"EpicenterLatitude": -30.2,
"EpicenterLongitude": 184.1
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年03月16日08時56分(臺灣時間),南太平洋 克馬德克群島發生規模7﹒1地震,震央位於西經175﹒90度、南緯30﹒20度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112003,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200301",
"EarthquakeInfo": {
"OriginTime": "2023-03-16 08:56:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "南太平洋 克馬德克群島",
"EpicenterLatitude": -30.2,
"EpicenterLongitude": 184.1
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.1
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對01月18日14時06分(臺灣時間)摩鹿加海規模7﹒0地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第2報",
"ReportType": "海嘯消息",
"TsunamiNo": 112002,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200202",
"EarthquakeInfo": {
"OriginTime": "2023-01-18 14:06:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 60,
"Epicenter": {
"Location": "摩鹿加海",
"EpicenterLatitude": 2.8,
"EpicenterLongitude": 127.1
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,2023年01月18日14時06分(臺灣時間),摩鹿加海發生規模7﹒2地震,震央位於東經127﹒10度、北緯2﹒80度。該中心研判可能在太平洋地區引發海嘯威脅,經初步推算,該地震所引發的海嘯,將不會影響臺灣。",
"ReportNo": "第1報",
"ReportType": "海嘯消息",
"TsunamiNo": 112002,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200201",
"EarthquakeInfo": {
"OriginTime": "2023-01-18 14:06:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 60,
"Epicenter": {
"Location": "摩鹿加海",
"EpicenterLatitude": 2.8,
"EpicenterLongitude": 127.1
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7.2
}
}
},
{
"ReportColor": "綠色",
"ReportContent": "頃獲太平洋海嘯警報中心通報,針對01月08日20時33分(臺灣時間)大洋洲 萬那杜群島規模7﹒0地震,依據該中心最新資訊,確認解除太平洋地區的海嘯威脅。",
"ReportNo": "第3報",
"ReportType": "海嘯消息",
"TsunamiNo": 112002,
"Web": "http://scweb.cwb.gov.tw/GraphicContent.aspx?ItemId=3&fileString=11200203",
"EarthquakeInfo": {
"OriginTime": "2023-01-08 20:33:00",
"Source": "美國太平洋海嘯警報中心",
"FocalDepth": 10,
"Epicenter": {
"Location": "大洋洲 萬那杜群島",
"EpicenterLatitude": -15.1,
"EpicenterLongitude": 166.7
},
"EarthquakeMagnitude": {
"MagnitudeValue": 7
}
}
}
]
}
}
```
:::
---
## ▼前置:
:::warning
:::spoiler ※===在開始前我們需要做點小事===※
#### 1.首先,我們需要開啟網路權限來使我們可以從網路上取得JSON文件。
在AndroidManifest.xml中加上`<uses-permission android:name="android.permission.INTERNET"/>`

#### 2.由於我們是由網路進行請求,所以最好開一個額外的執行續來拿資料,此處採用Coroutine
- [Android筆記–kotlin Coroutines(kotlin 協程)](https://hackmd.io/@9YAtszqXS2OLNZOrLY_-Jg/kotlin_coroutines)

#### 3.接下來將程式碼放在coroutine內就可以開始玩`json.org`了
:::
---
## ▼超簡單的用法:
總而言之就是一直`get資料類型(名稱)`就對了(?),例如: `getJSONObject("result")`
,請繼續往下看並嘗試模仿階段目標1~4就能簡單學會了。
---
## ▼階段目標1: 取得範例的內容

:::info
:::spoiler ※===解答===※
#### 1.我們要對JSON文件進行操作,需要將內容轉換成JSONObject或JSONArray的類型。
#### 2.因此我們使用`URL(網址)`來取得網站內容,用`readText()`表示我們要取得全部網站內容。
#### 3.最後用`JSONObject()`來將其轉換為JSONObject物件。


:::
---
## ▼階段目標2: 取得範例中的"result"裡的內容

:::success
:::spoiler ※===解答===※
#### 1.首先,根據[物件導向語言筆記--JSON](https://hackmd.io/@9YAtszqXS2OLNZOrLY_-Jg/OOP_JSON)我們可以知道,"result"屬於JSON物件
#### 2.因此,我們可以採用三種方法來拿到"result"的內容

:::
---
## ▼階段目標3: 取得範例中的"fields"裡的內容

:::info
:::spoiler ※===解答===※
#### 1.根據[物件導向語言筆記--JSON](https://hackmd.io/@9YAtszqXS2OLNZOrLY_-Jg/OOP_JSON)我們可以知道,有著`[]`的fields是一個 `JSON Array`,並且它是"result"下的一個`JSON Array`
#### 2.因此我們想拿到"fields"的資料就必須要給予正確的路徑,也就是
`整個JSON文件->中的"result"-> 中的"fields"`


:::
---
## ▼階段目標4: 取得範例中的"fields"中的ReportType"字串

:::success
:::spoiler ※===解答===※
#### 1.這次我們要取得的是,`"fields"中的 ->第一個JSONObject物件中的-> 鍵值的->值`
#### 2.照著路徑一路getXXX下去就好了

:::
---
## 備註1:其實每一個路徑中的最後一個getXXX,可以用`[]`來代替(僅限最後一個)
```
//例如:
val myJsonObjB = myJsonData.getJSONObject("result").getJSONArray("fields").getJSONObject(0).getString("id")
//等同於
val myJsonObjB = myJsonData.getJSONObject("result").getJSONArray("fields").getJSONObject(0)["id"]
//或是
val myJsonObjB = myJsonData.getJSONObject("result").getJSONArray("fields").getJSONObject(0)
//就等同於
val myJsonObjB = myJsonData.getJSONObject("result").getJSONArray("fields")[0]
```
## 備註2:JSON 資料解析時,如果我們沒有多加 has 或 isNull 來判斷欄位資料是否存在,會直接噴出例外錯誤。解決方法就是加上has 或 isNull 或是加上try.catch來防止
---
小工具:
---
- [jsonviewer](https://jsonviewer.stack.hu/):一個用來幫忙分析JSON文件的線上工具,可用來快速掌握整個 JSON 資料結構。
參考資料:
---
- [JSON.org官網](https://www.json.org/json-zh.html)
- ==★==[Android中轉換JSON的工具-GSON](https://jimmy4302001.medium.com/android%E4%B8%AD%E8%BD%89%E6%8F%9Bjson%E7%9A%84%E5%B7%A5%E5%85%B7-gson-3da217b58104)
- [android 解析JSON用法](https://jc7003.pixnet.net/blog/post/293480218)
- ==★==[[Android] 3-2 JSON的介紹和解析 - 給你魚竿(JAVA)](https://rx1226.pixnet.net/blog/post/245392108)
- [android Json详解- 超超boy](https://www.cnblogs.com/jycboy/p/json_xj.html)
- [Kotlin 的序列化工具](https://andyludeveloper.medium.com/kotlin-%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E5%B7%A5%E5%85%B7-8ee361b43b1b)
- [AndroidDeveloper--json.org](https://developer.android.com/reference/org/json/package-summary)
- ==★==[Day 9:JSON 資料解析](https://ithelp.ithome.com.tw/articles/10261711)
- [碼農日常-『Java&Kotlin』使用Gson解析JSON字串](https://thumbb13555.pixnet.net/blog/post/328611247-gsonconvert)