摘要:感冒指數(shù)易發(fā)感冒容易發(fā)生,少去人群密集的場所有利于降低感冒的幾率。穿衣指數(shù)舒適白天溫度適中,但早晚涼,易穿脫的便攜外套很實(shí)用。運(yùn)動(dòng)指數(shù)不適宜受到陣雨天氣的影響,不宜在戶外運(yùn)動(dòng)。
個(gè)人博客同步文章 https://mr-houzi.com/2018/06/...
根據(jù)一段天氣API來說一下JSONObject如何解析json數(shù)據(jù),盡管現(xiàn)在在開發(fā)中使用Gson等,對(duì)于像我這樣初次使用Java做開發(fā)的小白,說一下也是好的。JSON數(shù)據(jù)
選取這段json數(shù)據(jù)是因?yàn)檫@段數(shù)據(jù)還是比較復(fù)雜的,能涵蓋要說的關(guān)鍵點(diǎn)
{ "data": { "city": "深圳", "temphigh": "25", "templow": "19", "updatetime": "2017-11-04 13:23:00", "tempnow": "24", "sendibletemp": "27", "winddirect": "東北風(fēng)", "windpower": "2級(jí)", "humidity": "42", "sunrise": "06:29", "sunset": "17:45", "weather": "多云", "week": "星期六", "nl": null, "date": "2017-11-04", "index": [ { "name": "化妝指數(shù)", "level": "控油", "msg": "建議用露質(zhì)面霜打底,水質(zhì)無油粉底霜,透明粉餅,粉質(zhì)胭脂。" }, { "name": "感冒指數(shù)", "level": "易發(fā)", "msg": "感冒容易發(fā)生,少去人群密集的場所有利于降低感冒的幾率。" }, { "name": "洗車指數(shù)", "level": "不宜", "msg": "雨(雪)水和泥水會(huì)弄臟您的愛車,不適宜清洗車輛。" }, { "name": "穿衣指數(shù)", "level": "舒適", "msg": "白天溫度適中,但早晚涼,易穿脫的便攜外套很實(shí)用。" }, { "name": "紫外線強(qiáng)度指數(shù)", "level": "弱", "msg": "輻射較弱,涂擦SPF12-15、PA+護(hù)膚品。" }, { "name": "運(yùn)動(dòng)指數(shù)", "level": "不適宜", "msg": "受到陣雨天氣的影響,不宜在戶外運(yùn)動(dòng)。" } ], "pm25": { "aqi": 0, "co": 8, "o3": 42, "pm10": 63, "pm2_5": 64, "quality": "良", "so2": 4, "no2": 11, "updatetime": "2017-11-04 13:00:00" }, "daily": [ { "date": "2017-11-04", "week": "星期六", "sunrise": "06:29", "sunset": "17:45", "temphigh": "25", "templow": "19", "weather": "多云" }, { "date": "2017-11-05", "week": "星期日", "sunrise": "06:29", "sunset": "17:45", "temphigh": "26", "templow": "19", "weather": "多云" }, { "date": "2017-11-06", "week": "星期一", "sunrise": "06:29", "sunset": "17:45", "temphigh": "27", "templow": "20", "weather": "多云" }, { "date": "2017-11-07", "week": "星期二", "sunrise": "06:29", "sunset": "17:45", "temphigh": "28", "templow": "21", "weather": "多云" }, { "date": "2017-11-08", "week": "星期三", "sunrise": "06:29", "sunset": "17:45", "temphigh": "29", "templow": "22", "weather": "多云" }, { "date": "2017-11-09", "week": "星期四", "sunrise": "06:29", "sunset": "17:45", "temphigh": "28", "templow": "22", "weather": "多云" }, { "date": "2017-11-03", "week": "星期五", "sunrise": "06:29", "sunset": "17:45", "temphigh": "28", "templow": "18", "weather": "晴" } ] }, "status": 0, "msg": "ok" }解析JSON 利用JSONString進(jìn)行簡單解析
我利用了RxVolley進(jìn)行數(shù)據(jù)通信,t為API返回的數(shù)據(jù)
RxVolley.get("https://chkj02.market.alicloudapi.com/qgtq?city="+city, params, new HttpCallback() { @Override public void onSuccess(String t) { Loger.debug("請求到的數(shù)據(jù):" + t); } });
我們現(xiàn)在要獲取這部分?jǐn)?shù)據(jù),該如何進(jìn)行解析呢?
首先,將t中的數(shù)據(jù)傳到JSONObject類型的jsonObject中,再通過getJSONObject獲取到data下的數(shù)據(jù)。
//解析數(shù)據(jù) JSONObject jsonObject = new JSONObject(t); JSONObject jsonData = jsonObject.getJSONObject("data");
此時(shí),jsonData中數(shù)據(jù)為
{ "city": "深圳", "temphigh": "25", "templow": "19", "updatetime": "2017-11-04 13:23:00", "tempnow": "24", "sendibletemp": "27", "winddirect": "東北風(fēng)", "windpower": "2級(jí)", "humidity": "42", "sunrise": "06:29", "sunset": "17:45", "weather": "多云", "week": "星期六", "nl": null, "date": "2017-11-04" }
然后通過getString進(jìn)行讀值即可
//解析天氣 String jsonTemplow = jsonData.getString("templow"); String jsonTempHigh = jsonData.getString("temphigh"); String jsonWeather = jsonData.getString("weather"); String jsonTempnow = jsonData.getString("tempnow"); String jsonWinddirect = jsonData.getString("winddirect"); String jsonWindpower = jsonData.getString("windpower"); String jsonHumidity = jsonData.getString("humidity");利用JSONArray進(jìn)行復(fù)雜解析
這次,我們要獲取這部分?jǐn)?shù)據(jù)
首先,將t中的數(shù)據(jù)傳到JSONObject類型的jsonObject中,再通過getJSONObject獲取到data下的數(shù)據(jù)。然后jsonArray通過getJSONArray獲得index下的數(shù)據(jù)
//解析數(shù)據(jù) JSONObject jsonObject = new JSONObject(t); JSONObject jsonData = jsonObject.getJSONObject("data"); JSONArray jsonIndex =jsonData.getJSONArray("index"); JSONArray jsonDaily =jsonData.getJSONArray("daily");
方法一
此時(shí),jsonDaily中數(shù)據(jù)為
[ { "date": "2017-11-04", "week": "星期六", "sunrise": "06:29", "sunset": "17:45", "temphigh": "25", "templow": "19", "weather": "多云" }, { "date": "2017-11-05", "week": "星期日", "sunrise": "06:29", "sunset": "17:45", "temphigh": "26", "templow": "19", "weather": "多云" }, { "date": "2017-11-06", "week": "星期一", "sunrise": "06:29", "sunset": "17:45", "temphigh": "27", "templow": "20", "weather": "多云" }, { "date": "2017-11-07", "week": "星期二", "sunrise": "06:29", "sunset": "17:45", "temphigh": "28", "templow": "21", "weather": "多云" }, { "date": "2017-11-08", "week": "星期三", "sunrise": "06:29", "sunset": "17:45", "temphigh": "29", "templow": "22", "weather": "多云" }, { "date": "2017-11-09", "week": "星期四", "sunrise": "06:29", "sunset": "17:45", "temphigh": "28", "templow": "22", "weather": "多云" }, { "date": "2017-11-03", "week": "星期五", "sunrise": "06:29", "sunset": "17:45", "temphigh": "28", "templow": "18", "weather": "晴" } ]
把jsonDaily中按分類進(jìn)行解析,分為幾個(gè)ArrayList<>,dates、weeks、weathers等,然后進(jìn)行for循環(huán)。
Listdates = new ArrayList<>(); List weeks = new ArrayList<>(); List weathers = new ArrayList<>(); int j=1; for (int i=0;i 方法二
此時(shí),jsonIndex中數(shù)據(jù)為
[ { "name": "化妝指數(shù)", "level": "控油", "msg": "建議用露質(zhì)面霜打底,水質(zhì)無油粉底霜,透明粉餅,粉質(zhì)胭脂。" }, { "name": "感冒指數(shù)", "level": "易發(fā)", "msg": "感冒容易發(fā)生,少去人群密集的場所有利于降低感冒的幾率。" }, { "name": "洗車指數(shù)", "level": "不宜", "msg": "雨(雪)水和泥水會(huì)弄臟您的愛車,不適宜清洗車輛。" }, { "name": "穿衣指數(shù)", "level": "舒適", "msg": "白天溫度適中,但早晚涼,易穿脫的便攜外套很實(shí)用。" }, { "name": "紫外線強(qiáng)度指數(shù)", "level": "弱", "msg": "輻射較弱,涂擦SPF12-15、PA+護(hù)膚品。" }, { "name": "運(yùn)動(dòng)指數(shù)", "level": "不適宜", "msg": "受到陣雨天氣的影響,不宜在戶外運(yùn)動(dòng)。" } ]jsonArray為二維數(shù)組,我們通過兩個(gè)嵌套循環(huán)進(jìn)行遍歷。首先,外層根據(jù)數(shù)組長度進(jìn)行for循環(huán)遍歷;然后內(nèi)層使用迭代器進(jìn)行遍歷。
String[] jsonIndex = new String[20];//數(shù)組長度聲明為20確保夠用 int j=1; for (int i=0;i這樣此指數(shù)數(shù)據(jù)就被我們成功解析,然后存入jsonIndex數(shù)組中。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71311.html
摘要:概述即,是對(duì)象表示法的子集。具有以下特點(diǎn)數(shù)據(jù)放在鍵值對(duì)中數(shù)據(jù)由逗號(hào)分隔花括號(hào)表示對(duì)象方括號(hào)表示數(shù)組。創(chuàng)建一個(gè)對(duì)象為對(duì)象添加屬性創(chuàng)建數(shù)組將對(duì)象添加到數(shù)組將數(shù)組添加到對(duì)象將對(duì)象轉(zhuǎn)化成字符串參考文檔官網(wǎng) JSON概述 JSON即javascript object notation,是javascript對(duì)象表示法的子集。具有以下特點(diǎn): 數(shù)據(jù)放在鍵值對(duì)中; 數(shù)據(jù)由逗號(hào)分隔; 花括號(hào)表示對(duì)...
摘要:時(shí)間年月日星期日說明本文部分內(nèi)容均來自慕課網(wǎng)。慕課網(wǎng)教學(xué)示例源碼無個(gè)人學(xué)習(xí)源碼第一章課程概述課程介紹課程須知本課程面向所有使用語言進(jìn)行開發(fā)的小伙伴。 時(shí)間:2017年05月21日星期日說明:本文部分內(nèi)容均來自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)示例源碼:無個(gè)人學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:課程概述 1-1 ...
閱讀 3054·2021-09-03 10:33
閱讀 1278·2019-08-30 15:53
閱讀 2627·2019-08-30 15:45
閱讀 3389·2019-08-30 14:11
閱讀 541·2019-08-30 13:55
閱讀 2591·2019-08-29 15:24
閱讀 1921·2019-08-26 18:26
閱讀 3573·2019-08-26 13:41