天气查询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);
}
到此这篇天气预报接口 20天(天气预报接口 json)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/qdvuejs/11437.html