天气查询APP,两种
JSON解析方式
/**
* 原始
json数据解析
*
*/
//
JSONObject
jsonObject = new
JSONObject(res);
// String reason=
jsonObject.getString("reason");
// if (reason.equals("参数不正确")){
// handler.sendEmptyMessage(1);
// return;
// }
//
JSONObject result=
jsonObject.get
JSONObject("result");
//
JSONObject realtime=result.get
JSONObject("realtime");
//
JSONObject life=result.get
JSONObject("life");
//
JSONObject wind=realtime.get
JSONObject("wind");
// String time=realtime.getString("time");
//
JSONObject weather=realtime.get
JSONObject("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.get
JSONObject("info");
//
JSONArray kongtiao=info.get
JSONArray("kongtiao");
//
JSONArray yundong=info.get
JSONArray("yundong");
//
JSONArray ziwaixian=info.get
JSONArray("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().from
Json(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<String> kongtiao=infoBean.getKongtiao();
List<String> yundong=infoBean.getYundong();
List<String> ziwaixian=infoBean.getZiwaixian();
ACStr=kongtiao.get(0)+" "+kongtiao.get(1);
sportStr=yundong.get(0)+" "+yundong.get(1);
lightStr=ziwaixian.get(0)+" "+ziwaixian.get(1);
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/73129.html