天气预报接口 20天(天气预报接口 json)

天气预报接口 20天(天气预报接口 json)天气查询 APP 两种 JSON 解析方式 原始 json 数据解析 JSON Object json Object new JSON Object res String reason json Object getString reason if reason equals 参数不正确 handler sendEmptyMes 1 return

天气查询APP,两种

JSON

解析方式

/**

* 原始

json

数据解析

*

*/

//

JSON

Object

json

Object = new

JSON

Object(res);

// String reason=

json

Object.getString("reason");

// if (reason.equals("参数不正确")){

// handler.sendEmptyMessage(1);

// return;

// }

//

JSON

Object result=

json

Object.get

JSON

Object("result");

//

JSON

Object realtime=result.get

JSON

Object("realtime");

//

JSON

Object life=result.get

JSON

Object("life");

//

JSON

Object wind=realtime.get

JSON

Object("wind");

// String time=realtime.getString("time");

//

JSON

Object weather=realtime.get

JSON

Object("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");

//

JSON

Object info=life.get

JSON

Object("info");

//

JSON

Array kongtiao=info.get

JSON

Array("kongtiao");

//

JSON

Array yundong=info.get

JSON

Array("yundong");

//

JSON

Array ziwaixian=info.get

JSON

Array("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);

}

编程小号
上一篇 2025-03-20 10:27
下一篇 2025-08-11 12:40

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/73129.html