天气查询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 kongtiao=infoBean.getKongtiao();
List yundong=infoBean.getYundong();
List ziwaixian=infoBean.getZiwaixian();
ACStr=kongtiao.get(0)+" "+kongtiao.get(1);
sportStr=yundong.get(0)+" "+yundong.get(1);
lightStr=ziwaixian.get(0)+" "+ziwaixian.get(1);
}
2022-06-08 14:51:52
28.85MB
Json
1