用Gson解析复杂的天气Json数据

对于结构简单的Json数据用android集成的JSONArray和JSONObject就可以解析出来,当遇到比较比较复杂的数据时可以考虑用Gson,最近刚好在做一个关于天气的app,要从如下Json数据中得到自己想要的数据。Json数据如下:

{
    "error_code":0,
    "reason":"成功",
    "result":{
        "sk":{
            "temp":"27",
            "wind_direction":"西南风",
            "wind_strength":"3级",
            "humidity":"44",
            "time":"19:42"
        },
        "today":{
            "city":"北京",
            "date_y":"2015年08月18日",
            "week":"星期二",
            "temperature":"20~30",
            "weather":"雷阵雨",
            "fa":"04",
            "fb":"21",
            "wind":"无持续风向 微风",
            "dressing_index":"热",
            "dressing_advice":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。",
            "uv_index":"弱",
            "comfort_index":"--",
            "wash_index":"不宜",
            "travel_index":"较不宜",
            "exercise_index":"较不宜",
            "drying_index":"--"
        },
        "future":[
            {
                "temperature":"20~28",
                "weather":"雷阵雨",
                "fa":"04",
                "fb":"04",
                "wind":"无持续风向 微风",
                "week":"星期三",
                "date":"20150819"
            },
            {
                "temperature":"22~33",
                "weather":"多云",
                "fa":"01",
                "fb":"01",
                "wind":"无持续风向 微风",
                "week":"星期四",
                "date":"20150820"
            },
            {
                "temperature":"21~31",
                "weather":"雷阵雨",
                "fa":"04",
                "fb":"04",
                "wind":"无持续风向 微风",
                "week":"星期五",
                "date":"20150821"
            },
            {
                "temperature":"20~29",
                "weather":"雷阵雨",
                "fa":"04",
                "fb":"03",
                "wind":"无持续风向 微风",
                "week":"星期六",
                "date":"2015
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
天气查询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<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); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值