天天看點

和風天氣API資料分析

注冊和風天氣擷取key,請求資料時需要用到,具體可檢視 官方文檔

請求資料示例

3-10天天氣預報

{
    "HeWeather6": [
        {
            "basic": {
                "cid": "CN101010100",
                "location": "北京",
                "parent_city": "北京",
                "admin_area": "北京",
                "cnty": "中國",
                "lat": "39.90498734",
                "lon": "116.40528870",
                "tz": "8.0"
            },
            "daily_forecast": [
                {
                    "cond_code_d": "103",
                    "cond_code_n": "101",
                    "cond_txt_d": "晴間多雲",
                    "cond_txt_n": "多雲",
                    "date": "2017-10-26",
                    "hum": "57",
                    "pcpn": "0.0",
                    "pop": "0",
                    "pres": "1020",
                    "tmp_max": "16",
                    "tmp_min": "8",
                    "uv_index": "3",
                    "vis": "16",
                    "wind_deg": "0",
                    "wind_dir": "無持續風向",
                    "wind_sc": "微風",
                    "wind_spd": "5"
                },
                {
                    "cond_code_d": "101",
                    "cond_code_n": "501",
                    "cond_txt_d": "多雲",
                    "cond_txt_n": "霧",
                    "date": "2017-10-27",
                    "hum": "56",
                    "pcpn": "0.0",
                    "pop": "0",
                    "pres": "1018",
                    "tmp_max": "18",
                    "tmp_min": "9",
                    "uv_index": "3",
                    "vis": "20",
                    "wind_deg": "187",
                    "wind_dir": "南風",
                    "wind_sc": "微風",
                    "wind_spd": "6"
                },
                {
                    "cond_code_d": "101",
                    "cond_code_n": "101",
                    "cond_txt_d": "多雲",
                    "cond_txt_n": "多雲",
                    "date": "2017-10-28",
                    "hum": "26",
                    "pcpn": "0.0",
                    "pop": "0",
                    "pres": "1029",
                    "tmp_max": "17",
                    "tmp_min": "5",
                    "uv_index": "2",
                    "vis": "20",
                    "wind_deg": "2",
                    "wind_dir": "北風",
                    "wind_sc": "3-4",
                    "wind_spd": "19"
                }
            ],
            "status": "ok",
            "update": {
                "loc": "2017-10-26 23:09",
                "utc": "2017-10-26 15:09"
            }
        }
    ]
}           

複制

傳回的資料為data,object表示對象,array表示數組:

data object
HeWeather6 array
HeWeather6數組第一項 object
basic object
daily_forecast array
daily_forecast數組中 object

解碼示例:

String data=資料
JSONObject jo=new JSONObject(data);
JSONArray ja=jo.getJSONArray("HeWeather6");
JSONObject joo=ja.getJSONObject(0);
JSONArray daily=joo.getJSONArray("daily_forecast");
for(int i=0;i<daily.length();i++)
{
     JSONObject day=daily.getJSONObject(i);
     String date=day.getString("date");
     String weather=day.getString("cond_txt_d");
     //擷取其他資料           

複制