Json 數據處理
首先可以在https://jsonformatter.curiousconcept.com 這個網站上將Json數據格式化一下,看着比較方便,分層很明確。
JSONObject weatherJson = new JSONObject(forecastString);
final String LIST = "HeWeather data service 3.0";
final String DAY = "daily_forecast";
final String DATE = "date";
final String TEMP = "tmp";
final String COND = "cond";
JSONArray weatherArr = weatherJson.getJSONArray(LIST);
JSONArray dayArray = weatherArr.getJSONObject(0).getJSONArray(DAY);
String [] results = new String[dayArray.length()];
for (int i = 0; i < dayArray.length();i++){
String date;
String temp;
String weatherDes;
date = dayArray.getJSONObject(i).getString(DATE);
temp = dayArray.getJSONObject(i).getJSONObject(TEMP).getString("max");
temp += "/"+dayArray.getJSONObject(i).getJSONObject(TEMP).getString("min");
weatherDes = dayArray.getJSONObject(i).getJSONObject(COND).getString("txt_d");
results[i]= date+" "+temp+" "+weatherDes;
Log.e("i=",i+" "+results[i]);
}
return results;
}
取回的數據爲:
i=: 0 2016-04-13 27/16 多雲
i=: 1 2016-04-14 27/15 多雲
i=: 2 2016-04-15 24/17 多雲
i=: 3 2016-04-16 21/13 中雨
i=: 4 2016-04-17 23/14 多雲
i=: 5 2016-04-18 21/12 雷陣雨
i=: 6 2016-04-19 22/14 多雲