文件名称:天气查询APP,Json解析,3种方式
文件大小:28.85MB
文件格式:RAR
更新时间:2020-10-15 06:45:47
Json
天气查询APP,两种JSON解析方式
/**
* 原始json数据解析
*
*/
// JSONObject jsonObject = new JSONObject(res);
// String reason=jsonObject.getString("reason");
// if (reason.equals("参数不正确")){
// handler.sendEmptyMessage(1);
// return;
// }
// JSONObject result=jsonObject.getJSONObject("result");
// JSONObject realtime=result.getJSONObject("realtime");
// JSONObject life=result.getJSONObject("life");
// JSONObject wind=realtime.getJSONObject("wind");
// String time=realtime.getString("time");
// JSONObject weather=realtime.getJSONObject("weather");
// String date=realtime.getString("date");
// dateStr=time+date;
// weekStr=realtime.getString("week");
// calendarStr=realtime.getString("moon");
// windpowerStr=wind.getString("direct")+" "+wind.getString("power");
// weatherStr=weather.getString("info");
// temperatureStr=weather.getString("temperature");
// JSONObject info=life.getJSONObject("info");
// JSONArray kongtiao=info.getJSONArray("kongtiao");
// JSONArray yundong=info.getJSONArray("yundong");
// JSONArray ziwaixian=info.getJSONArray("ziwaixian");
// ACStr=kongtiao.getString(0)+" "+kongtiao.getString(1);
// sportStr=yundong.getString(0)+" "+yundong.getString(1);
// lightStr=ziwaixian.getString(0)+" "+ziwaixian.getString(1);
/**
* Gson数据解析
*/
WheatherBean wheatherBean=new Gson().fromJson(res,WheatherBean.class);
String reason=wheatherBean.getReason();
if (reason.equals("参数不正确")){
handler.sendEmptyMessage(1);
return;
}
WheatherBean.ResultBean resultBean=wheatherBean.getResult();
WheatherBean.ResultBean.RealtimeBean realtimeBean=resultBean.getRealtime();
WheatherBean.ResultBean.RealtimeBean.WindBean windBean=realtimeBean.getWind();
String time=realtimeBean.getTime();
WheatherBean.ResultBean.RealtimeBean.WeatherBean weatherBean=realtimeBean.getWeather();
String date=realtimeBean.getDate();
dateStr=time+date;
weekStr=realtimeBean.getWeek();
calendarStr=realtimeBean.getMoon();
windpowerStr=windBean.getDirect()+" "+windBean.getPower();
temperatureStr=weatherBean.getTemperature();
weatherStr=weatherBean.getInfo();
WheatherBean.ResultBean.LifeBean lifeBean=resultBean.getLife();
WheatherBean.ResultBean.LifeBean.InfoBean infoBean=lifeBean.getInfo();
List