經過http請求獲取返回結果,結果類型爲string,先轉換爲json,java
JSONObject jsonObject=JSONObject.parseObject(result);
json數據結果以下: json
{ "data": { "city": "深圳", "temphigh": "25", "templow": "19", "updatetime": "2017-11-04 13:23:00", "tempnow": "24", "sendibletemp": "27", "winddirect": "東北風", "windpower": "2級", "humidity": "42", "sunrise": "06:29", "sunset": "17:45", "weather": "多雲", "week": "星期六", "nl": null, "date": "2017-11-04", "index": [ { "name": "化妝指數", "level": "控油", "msg": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。" }, { "name": "感冒指數", "level": "易發", "msg": "感冒容易發生,少去人羣密集的場全部利於下降感冒的概率。" }, { "name": "洗車指數", "level": "不宜", "msg": "雨(雪)水和泥水會弄髒您的愛車,不適宜清洗車輛。" }, { "name": "穿衣指數", "level": "溫馨", "msg": "白天溫度適中,但遲早涼,易穿脫的便攜外套很實用。" }, { "name": "紫外線強度指數", "level": "弱", "msg": "輻射較弱,塗擦SPF12-1五、PA+護膚品。" }, { "name": "運動指數", "level": "不適宜", "msg": "受到陣雨天氣的影響,不宜在戶外運動。" } ], "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" }
獲取以下內容,如何解析?數組
{ "data": { "city": "深圳", "temphigh": "25", "templow": "19", "updatetime": "2017-11-04 13:23:00", "tempnow": "24", "sendibletemp": "27", "winddirect": "東北風", "windpower": "2級", "humidity": "42", "sunrise": "06:29", "sunset": "17:45", "weather": "多雲", "week": "星期六", "nl": null, "date": "2017-11-04",
經過getJSONObject
獲取到data
下的數據。spa
JSONObject jsonData = jsonObject.getJSONObject("data");
此時,jsonData中數據爲指針
{ "city": "深圳", "temphigh": "25", "templow": "19", "updatetime": "2017-11-04 13:23:00", "tempnow": "24", "sendibletemp": "27", "winddirect": "東北風", "windpower": "2級", "humidity": "42", "sunrise": "06:29", "sunset": "17:45", "weather": "多雲", "week": "星期六", "nl": null, "date": "2017-11-04" }
而後經過getString
進行讀值便可code
//解析天氣 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");
獲取這部分數據內存
"index": [ { "name": "化妝指數", "level": "控油", "msg": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。" }, { "name": "感冒指數", "level": "易發", "msg": "感冒容易發生,少去人羣密集的場全部利於下降感冒的概率。" }, { "name": "洗車指數", "level": "不宜", "msg": "雨(雪)水和泥水會弄髒您的愛車,不適宜清洗車輛。" }, { "name": "穿衣指數", "level": "溫馨", "msg": "白天溫度適中,但遲早涼,易穿脫的便攜外套很實用。" }, { "name": "紫外線強度指數", "level": "弱", "msg": "輻射較弱,塗擦SPF12-1五、PA+護膚品。" }, { "name": "運動指數", "level": "不適宜", "msg": "受到陣雨天氣的影響,不宜在戶外運動。" } ], "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": "晴" } ]
經過getJSONObject
獲取到data
下的數據。而後jsonArray經過getJSONArray
得到index
下的數據ci
//解析數據 JSONObject jsonObject = new JSONObject(t); JSONObject jsonData = jsonObject.getJSONObject("data"); JSONArray jsonIndex =jsonData.getJSONArray("index"); JSONArray jsonDaily =jsonData.getJSONArray("daily");
方法一get
此時,jsonDaily中數據爲string
[ { "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中按分類進行解析,分爲幾個ArrayList<>,dates
、weeks
、weathers
等,而後進行for循環。
List<String> dates = new ArrayList<>(); List<String> weeks = new ArrayList<>(); List<String> weathers = new ArrayList<>(); int j=1; for (int i=0;i<jsonArray.length();i++){ JSONObject partDaily = jsonArray.getJSONObject(i); JSONString date = partDaily.getString("date"); dates.add(date); JSONString week = partDaily.getString("week"); weeks.add(week); JSONString weather = partDaily.getString("weather"); weathers.add(weather); }
方法二
此時,jsonIndex中數據爲
[ { "name": "化妝指數", "level": "控油", "msg": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。" }, { "name": "感冒指數", "level": "易發", "msg": "感冒容易發生,少去人羣密集的場全部利於下降感冒的概率。" }, { "name": "洗車指數", "level": "不宜", "msg": "雨(雪)水和泥水會弄髒您的愛車,不適宜清洗車輛。" }, { "name": "穿衣指數", "level": "溫馨", "msg": "白天溫度適中,但遲早涼,易穿脫的便攜外套很實用。" }, { "name": "紫外線強度指數", "level": "弱", "msg": "輻射較弱,塗擦SPF12-1五、PA+護膚品。" }, { "name": "運動指數", "level": "不適宜", "msg": "受到陣雨天氣的影響,不宜在戶外運動。" } ]
jsonArray爲二維數組,咱們經過兩個嵌套循環進行遍歷。首先,外層根據數組長度進行for循環遍歷;而後內層使用迭代器進行遍歷。
String[] jsonIndex = new String[20];//數組長度聲明爲20確保夠用 int j=1; for (int i=0;i<jsonArray.length();i++){ JSONObject partIndex = jsonArray.getJSONObject(i); Iterator iterator = partIndex.keys(); String key; while(iterator.hasNext()){ //hasNext方法,只是判斷下一個元素的有無,並不移動指針 key = (String) iterator.next();//next方法,向下移動指針,而且返回指針指向的元素,若是指針指向的內存中沒有元素,會報異常 jsonIndex[j] = partIndex.getString(key); j++; } }
這樣此指數數據就被咱們成功解析,而後存入jsonIndex數組中。