zoukankan      html  css  js  c++  java
  • 解析Json数据

    简单Json用:JSONObject   

    嵌套的复杂Json用:GSON

    JSONObject  :

     private void parseJSON(String jsonData) {
                try {
                    JSONArray jsonArray = new JSONArray(jsonData);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        String id =jsonObject.getString("ID");
                        Log.d("AA",id);
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            }

    就是这么简单:

    Gson 解析嵌套Json数据

    Gson2.8.2  链接:https://pan.baidu.com/s/1eRFt9Nc 密码:41sl

    Json天气数据

    {
        "HeWeather6": [
            {
                "basic": {
                    "cid": "CN101271506",
                    "location": "青神",
                    "parent_city": "眉山",
                    "admin_area": "四川",
                    "cnty": "中国",
                    "lat": "29.83146858",
                    "lon": "103.84613037",
                    "tz": "+8.0"
                },
                "update": {
                    "loc": "2018-01-09 08:53",
                    "utc": "2018-01-09 00:53"
                },
                "status": "ok",
                "now": {
                    "cloud": "5",
                    "cond_code": "101",
                    "cond_txt": "多云",
                    "fl": "-6",
                    "hum": "89",
                    "pcpn": "0.0",
                    "pres": "1031",
                    "tmp": "0",
                    "vis": "7",
                    "wind_deg": "138",
                    "wind_dir": "东南风",
                    "wind_sc": "微风",
                    "wind_spd": "6"
                },
                "daily_forecast": [
                    {
                        "cond_code_d": "101",
                        "cond_code_n": "101",
                        "cond_txt_d": "多云",
                        "cond_txt_n": "多云",
                        "date": "2018-01-09",
                        "hum": "51",
                        "mr": "01:02",
                        "ms": "13:09",
                        "pcpn": "0.0",
                        "pop": "0",
                        "pres": "1031",
                        "sr": "08:01",
                        "ss": "18:24",
                        "tmp_max": "11",
                        "tmp_min": "0",
                        "uv_index": "4",
                        "vis": "20",
                        "wind_deg": "0",
                        "wind_dir": "无持续风向",
                        "wind_sc": "微风",
                        "wind_spd": "3"
                    },
                    {
                        "cond_code_d": "100",
                        "cond_code_n": "101",
                        "cond_txt_d": "晴",
                        "cond_txt_n": "多云",
                        "date": "2018-01-10",
                        "hum": "46",
                        "mr": "01:58",
                        "ms": "13:45",
                        "pcpn": "0.0",
                        "pop": "0",
                        "pres": "1033",
                        "sr": "08:01",
                        "ss": "18:25",
                        "tmp_max": "12",
                        "tmp_min": "0",
                        "uv_index": "4",
                        "vis": "20",
                        "wind_deg": "0",
                        "wind_dir": "无持续风向",
                        "wind_sc": "微风",
                        "wind_spd": "3"
                    },
                    {
                        "cond_code_d": "100",
                        "cond_code_n": "101",
                        "cond_txt_d": "晴",
                        "cond_txt_n": "多云",
                        "date": "2018-01-11",
                        "hum": "45",
                        "mr": "02:52",
                        "ms": "14:20",
                        "pcpn": "0.0",
                        "pop": "0",
                        "pres": "1032",
                        "sr": "08:01",
                        "ss": "18:25",
                        "tmp_max": "11",
                        "tmp_min": "0",
                        "uv_index": "4",
                        "vis": "20",
                        "wind_deg": "0",
                        "wind_dir": "无持续风向",
                        "wind_sc": "微风",
                        "wind_spd": "3"
                    }
                ],
                "hourly": [
                    {
                        "cloud": "7",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-5",
                        "hum": "46",
                        "pop": "0",
                        "pres": "1033",
                        "time": "2018-01-09 10:00",
                        "tmp": "1",
                        "wind_deg": "48",
                        "wind_dir": "东北风",
                        "wind_sc": "微风",
                        "wind_spd": "3"
                    },
                    {
                        "cloud": "5",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-7",
                        "hum": "32",
                        "pop": "0",
                        "pres": "1030",
                        "time": "2018-01-09 13:00",
                        "tmp": "4",
                        "wind_deg": "81",
                        "wind_dir": "东风",
                        "wind_sc": "微风",
                        "wind_spd": "5"
                    },
                    {
                        "cloud": "4",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-7",
                        "hum": "28",
                        "pop": "0",
                        "pres": "1028",
                        "time": "2018-01-09 16:00",
                        "tmp": "10",
                        "wind_deg": "100",
                        "wind_dir": "东风",
                        "wind_sc": "微风",
                        "wind_spd": "4"
                    },
                    {
                        "cloud": "6",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-5",
                        "hum": "37",
                        "pop": "0",
                        "pres": "1032",
                        "time": "2018-01-09 19:00",
                        "tmp": "8",
                        "wind_deg": "223",
                        "wind_dir": "西南风",
                        "wind_sc": "微风",
                        "wind_spd": "4"
                    },
                    {
                        "cloud": "7",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-5",
                        "hum": "45",
                        "pop": "0",
                        "pres": "1036",
                        "time": "2018-01-09 22:00",
                        "tmp": "3",
                        "wind_deg": "335",
                        "wind_dir": "西北风",
                        "wind_sc": "微风",
                        "wind_spd": "7"
                    },
                    {
                        "cloud": "4",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-4",
                        "hum": "53",
                        "pop": "0",
                        "pres": "1035",
                        "time": "2018-01-10 01:00",
                        "tmp": "2",
                        "wind_deg": "355",
                        "wind_dir": "北风",
                        "wind_sc": "微风",
                        "wind_spd": "4"
                    },
                    {
                        "cloud": "5",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-4",
                        "hum": "57",
                        "pop": "0",
                        "pres": "1033",
                        "time": "2018-01-10 04:00",
                        "tmp": "0",
                        "wind_deg": "142",
                        "wind_dir": "东南风",
                        "wind_sc": "微风",
                        "wind_spd": "2"
                    },
                    {
                        "cloud": "5",
                        "cond_code": "103",
                        "cond_txt": "晴间多云",
                        "dew": "-5",
                        "hum": "58",
                        "pop": "0",
                        "pres": "1034",
                        "time": "2018-01-10 07:00",
                        "tmp": "0",
                        "wind_deg": "41",
                        "wind_dir": "东北风",
                        "wind_sc": "微风",
                        "wind_spd": "3"
                    }
                ],
                "lifestyle": [
                    {
                        "brf": "较舒适",
                        "txt": "白天虽然天气晴好,但早晚会感觉偏凉,午后舒适、宜人。",
                        "type": "comf"
                    },
                    {
                        "brf": "较冷",
                        "txt": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。",
                        "type": "drsg"
                    },
                    {
                        "brf": "易发",
                        "txt": "昼夜温差很大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。",
                        "type": "flu"
                    },
                    {
                        "brf": "较适宜",
                        "txt": "天气较好,无雨水困扰,较适宜进行各种运动,但因气温较低,在户外运动请注意增减衣物。",
                        "type": "sport"
                    },
                    {
                        "brf": "适宜",
                        "txt": "天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。",
                        "type": "trav"
                    },
                    {
                        "brf": "最弱",
                        "txt": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。",
                        "type": "uv"
                    },
                    {
                        "brf": "较适宜",
                        "txt": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。",
                        "type": "cw"
                    },
                    {
                        "brf": "较差",
                        "txt": "气象条件较不利于空气污染物稀释、扩散和清除,请适当减少室外活动时间。",
                        "type": "air"
                    }
                ],
                "air_now_city": {
                    "aqi": "73",
                    "qlty": "良",
                    "main": "PM2.5",
                    "pm25": "53",
                    "pm10": "83",
                    "no2": "33",
                    "so2": "6",
                    "co": "0.8",
                    "o3": "22",
                    "pub_time": "2018-01-09 07:30"
                }
            }
        ]
    }

    创建对应的实体类(SerializedName是Json数据的名,下面是对应的自定义名

    /**
     * 基本信息
     */
    public class BasicJson {
    
        @SerializedName("cid")
        public String CityWeatherID;   ///城市和风天气ID
    
        @SerializedName("location")
        public String CityName;        //城市名称
    
        @SerializedName("parent_city")
        public String Parent_City;        //城市的上级城市
    
        @SerializedName("admin_area")
        public String Admin_Area;        //城市所属行政区域
    
        @SerializedName("cnty")
        public String Cnty;        //城市所属国家名称
    
        @SerializedName("lat")
        public String Lat;        //城市经度
    
        @SerializedName("lon")
        public String Lon;        //城市纬度
    
        @SerializedName("tz")
        public String Tz;        //城市所在时区
    
    
    }
    public class LifeStyleJson {
    
        @SerializedName("brf")
        public String Brf;           ///生活指数简介
    
        @SerializedName("txt")
        public String Txt;           ///生活指数详细描述
    
    
        ///生活指数类型 comf:舒适度指数、cw:洗车指数、drsg:穿衣指数、
        ///flu:感冒指数、sport:运动指数、trav:旅游指数、uv:紫外线指数、air:空气污染扩散条件指数
        @SerializedName("type")
        public String Type;
    
    
    }

    按照以上两个创建对应的实体就行

    再加一个Json总的实体(如上面的LifeStyleJson ,是个集合则在下面总实体类中用List声明)

    public class ListJson {
    
        @SerializedName("basic")
        public BasicJson Basic;         ///城市信息
    
        @SerializedName("update")
        public UpdateJson Update;       ///更新时间
    
        @SerializedName("status")
        public String Status;           ///状态码
    
    
        @SerializedName("now")
        public WeatherJson Now;            ///实况天气
    
        @SerializedName("daily_forecast")
        public List<DailyForecastJson> DailyForecast;    ///3天天气预报
    
        @SerializedName("hourly")
        public List<HourJson> HourLy;
    
        @SerializedName("lifestyle")
        public List<LifeStyleJson> LifeStyle; ///生活指数
    
    
        @SerializedName("air_now_city")
        public AQIJson AirNowCity;      ///空气质量实况

    创建解析方法

    public static ListJson HandleWeatherResponse(String ResponseStr) {
            try {
                JSONObject WeatherJsonList = new JSONObject(ResponseStr);
                JSONArray WeatherArray = WeatherJsonList.getJSONArray("HeWeather6");
                String WeatherContent = WeatherArray.getJSONObject(0).toString();
                return new Gson().fromJson(WeatherContent, ListJson.class);     //转换成Json对象
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

    提取数据

    //展示数据
        public void ShowWeatherInfo(ListJson Weatherobject) {
            if (Weatherobject != null && "ok".equals(Weatherobject.Status)) {
                String Name = Weatherobject.Basic.CityName;    
                String UpdateTime = Weatherobject.Update.Utc;
                String Degree = Weatherobject.Now.Tmp + "℃";
                String Cond = Weatherobject.Now.CondTxt;
    
            //集合数据
    for (DailyForecastJson Forecast : Weatherobject.DailyForecast) {
              DateTxt.setText(Forecast.Date);
              WeatherInfo.setText(Forecast.CondTxtD);
              MaxTxt.setText(Forecast.TmpMax);
              MinTxt.setText(Forecast.TmpMin);
            } 

            if (Weatherobject.AirNowCity.Aqi != null) {
      AqiTxt.setText(Weatherobject.AirNowCity.Aqi);
      Pm25Txt.setText(Weatherobject.AirNowCity.Pm25);
       }

       for (LifeStyleJson Life : Weatherobject.LifeStyle) {
       if (Life.Type.equals("comf")) {
       String Comfort = "舒适度:" + Life.Txt;
       ComfTxt.setText(Comfort);
       }
      }

    }


  • 相关阅读:
    C语言的数组,指针,二级指针,指针数组和数组指针的简单理解
    bash shell 中时间操作常用方法总结
    常见字符串操作方式总结
    查看机器负载常用姿势总结
    netstat命令常用总结
    【技术累积】【点】【java】【20】static关键字
    【技术累积】【点】【java】【19】访问权限
    【技术累积】【点】【java】【18】URLEncode
    【技术累积】【点】【sql】【17】了解索引
    【技术累积】【点】【算法】【17】算法的时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/xinyibufang/p/8249985.html
Copyright © 2011-2022 走看看