Unity 工具之 獲取當前所在城市的天氣數據的封裝(自動定位當前所在城市,天氣數據可以獲得多天天數據)

 

 

Unity 工具之 獲取當前所在城市的天氣數據的封裝(自動定位當前所在城市,天氣數據可以獲得多天天數據)

 

目錄

Unity 工具之 獲取當前所在城市的天氣數據的封裝(自動定位當前所在城市,天氣數據可以獲得多天天數據)

一、簡單介紹

二、實現原理

三、注意實現

四、效果預覽

五、實現步驟

六、關鍵代碼

七、工程參考

八、參考文章(十分感謝這些博主的文章)

九、附:一些免費、穩定的天氣預報 API 的資源參考學習

1. 國家氣象局

2. 中國天氣SmartWeatherAPI(http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml)

3. 和風天氣

4. 心知天氣(免費版只提供地級市數據)

5. 彩雲天氣

6. 中央天氣預報


 

一、簡單介紹

Unity 工具類,自己整理的一些遊戲開發可能用到的模塊,單獨獨立使用,方便遊戲開發。

本屆介紹獲取所在城市的天氣數據,並且封裝成數據接口,只要設置一個監聽數據接口,即可調用。

 

二、實現原理

1、首先根據聯網 IP 獲得當前所在城市

2、然後根據城市名稱得到天氣所需要的城市ID;

3、根據城市ID,最後得到天氣數據,解析封裝給接口調用;

 

三、注意實現

1、所以使用時候需要連接網絡

2、接口目前好似只能獲得國內的城市天氣數據

 

四、效果預覽

 

五、實現步驟

1、打開Unity,新建一個空工程,導入LitJson 插件

 

2、在場景中佈局UI,展示一些天氣數據

 

3、新建幾個腳本,其中幾個腳本定義一些數據結構(PositionDataStruct,WeatherDataStruct),GetWeatherWrapper 主要實現定位當前城市,和獲取天氣數據,TestGetWeatherWrapper 測試 GetWeatherWrapper 的接口,並展示一些天氣數據

 

4、運行場景,效果如下

 

六、關鍵代碼

1、PositionDataStruct

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PositionDataStruct
{
   

    public class ResponseBody
    {

        public string address;
        public Content content;
        public int status;

    }

    public class Content
    {
        public string address;
        public Address_Detail address_detail;
        public Point point;
    }
    public class Address_Detail
    {
        public string city;
        public int city_code;
        public string district;
        public string province;
        public string street;
        public string street_number;

        public Address_Detail()
        {

        }

        public Address_Detail(string city, int city_code, string district, string province, string street, string street_number)
        {
            this.city = city;
            this.city_code = city_code;
            this.district = district;
            this.province = province;
            this.street = street;
            this.street_number = street_number;
        }
    }

    public class Point
    {
        public string x;
        public string y;

        public Point()
        {

        }

        public Point(string x, string y)
        {
            this.x = x;
            this.y = y;
        }
    }
    

}

 

2、WeatherDataStruct

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeatherDataStruct
{
    public static Dictionary<string, int> PosToId = new Dictionary<string, int>();

    public static bool initDic = false;

    public const string WeatherCityId = "WeatherCityId";

    public static int GetWeatherId(string name)
    {
        int id = 0;
        if(!initDic)
        {
            initDic = true;
            TextAsset ta = Resources.Load<TextAsset>(WeatherCityId);
            List<Pos2Id> temp = LitJson.JsonMapper.ToObject<List<Pos2Id>>(ta.text);
            foreach(Pos2Id t in temp)
            {
                PosToId[t.placeName] = t.id;
            }
        }
        for(int i=1;i<name.Length;i++)
        {
            string tn = name.Substring(0, i);
            if(PosToId.ContainsKey(tn))
            {
                id = PosToId[tn];
            }
        }
        return id;
    }

    public class Pos2Id
    {
        public string placeName;
        public int id;

        public Pos2Id()
        {

        }
        public Pos2Id(string name,int id)
        {
            placeName = name;
            this.id = id;
        }
    }

    public class WeathBody
    {
        public string time;
        public CityInfo cityInfo;
        public string date;
        public string message;
        public int status;
        public WeathData data;
    }

    public class CityInfo
    {
        public string city;
        public string citykey;
        public string parent;
        public string updateTime;
    }

    public class WeathData
    {
        public string shidu;
        public double pm25;
        public double pm10;
        public string quality;
        public string wendu;
        public string ganmao;
        public WeathDetailData yesterday;
        public WeathDetailData[] forecast;
    }

    public class WeathDetailData
    {
        public string date;
        public string sunrise;
        public string high;
        public string low;
        public string sunset;
        public double aqi;
        public string ymd;
        public string week;
        public string fx;
        public string fl;
        public string type;
        public string notice;
    }

}

 

3、GetWeatherWrapper

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 獲取對應城市的天氣
/// 注意:溫度時攝氏度°C
/// </summary>
public class GetWeatherWrapper : MonoSingleton<GetWeatherWrapper>
{
    /// <summary>
    /// 獲取位置信息
    /// </summary>
    string PostionUrl = "http://api.map.baidu.com/location/ip?ak=bretF4dm6W5gqjQAXuvP0NXW6FeesRXb&coor=bd09ll";

    /// <summary>
    /// 獲取天氣
    /// </summary>
    string WeatherUrl = "http://t.weather.sojson.com/api/weather/city/";


    Action<WeatherDataStruct.WeathBody> WeatherDataEvent;

    // 多少秒後更新一次數據 
    private float updateWeatherTime = 30;

    public float UpdateWeatherTime { get => updateWeatherTime; set => updateWeatherTime = value; }

    void Start()
    {
        //獲取位置
        StartCoroutine(RequestPos());
    }


    /// <summary>
    /// 設置天氣數據的監聽事件
    /// </summary>
    /// <param name="WeatherDataAction"></param>
    public void SetWeatherDataEvent(Action<WeatherDataStruct.WeathBody> WeatherDataAction) {
        WeatherDataEvent = WeatherDataAction;
    }

    /// <summary>
    /// 獲取當前所在城市
    /// </summary>
    /// <returns></returns>
    IEnumerator RequestPos()
    {
        WWW www = new WWW(PostionUrl);
        yield return www;

        if (string.IsNullOrEmpty(www.error))
        {
            PositionDataStruct.ResponseBody t = LitJson.JsonMapper.ToObject<PositionDataStruct.ResponseBody>(www.text);
            Debug.Log(t.content.address_detail.city);
            //獲取天氣
            StartCoroutine(RequestWeather(WeatherDataStruct.GetWeatherId(t.content.address_detail.city)));
        }
    }

    /// <summary>
    /// 獲取當前所在城市的天氣數據
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    IEnumerator RequestWeather(int id)
    {
        yield return new WaitForEndOfFrame();

        while (true) {
            WWW www = new WWW(WeatherUrl + id.ToString());
            Debug.Log(WeatherUrl + id.ToString());
            yield return www;

            if (string.IsNullOrEmpty(www.error))
            {
                Debug.Log(www.text);
                WeatherDataStruct.WeathBody t = LitJson.JsonMapper.ToObject<WeatherDataStruct.WeathBody>(www.text);
                Debug.Log(t.data.forecast[0].notice);
                if (WeatherDataEvent != null)
                {
                    for (int i = 0; i < t.data.forecast.Length; i++)
                    {
                        // 正則表達式獲取溫度數值
                        string low = System.Text.RegularExpressions.Regex.Replace(t.data.forecast[i].low, @"[^0-9]+", "");
                        string high = System.Text.RegularExpressions.Regex.Replace(t.data.forecast[i].high, @"[^0-9]+", "");
                        t.data.forecast[i].low = low;
                        t.data.forecast[i].high = high;
                    }

                    WeatherDataEvent.Invoke(t);
                }
            }

            yield return new WaitForSeconds(updateWeatherTime);
        }

        
    }
}

 

4、TestGetWeatherWrapper

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestGetWeatherWrapper : MonoBehaviour
{
    public Text City_Text;
    public Text WeatherDec_Text;
    public Text Temperature_Text;
    public Text WeatherTime_Text;
    public Text WeatherNotice_Text;


    // Start is called before the first frame update
    void Start()
    {
        ShowWeather();
    }


    void ShowWeather() {
        GetWeatherWrapper.Instance.SetWeatherDataEvent((weatherData)=> {

            City_Text.text = weatherData.cityInfo.city;
            WeatherTime_Text.text = weatherData.date;
            WeatherDec_Text.text = weatherData.data.forecast[0].type;
            Temperature_Text.text = weatherData.data.forecast[0].low +"°C"+ "~" + weatherData.data.forecast[0].high + "°C";
            WeatherNotice_Text.text = weatherData.data.forecast[0].notice;
        });
    }
}

 

七、工程參考

下載地址:https://download.csdn.net/download/u014361280/12568067

 

八、參考文章(十分感謝這些博主的文章)

1、https://blog.csdn.net/SnoopyNa2Co3/article/details/89853201

2、https://www.jianshu.com/p/e3e04cf3fc0f

 

九、附:一些免費、穩定的天氣預報 API 的資源參考學習

1. 國家氣象局

1)實時接口:

實時天氣1:http://www.weather.com.cn/data/sk/101190408.html

實時天氣2:http://www.weather.com.cn/data/cityinfo/101190408.html

實時天氣3(帶時間戳):http://mobile.weather.com.cn/data/sk/101010100.html?_=1381891661455

2)一周天氣預報接口

7天預報數據 URL: http://mobile.weather.com.cn/data/forecast/101010100.html?_=1381891660081

該接口來源氣象局移動版網站,json數據格式如下:

{
    "c": {
        "c1": "101010100", 
        "c2": "beijing", 
        "c3": "北京", 
        "c4": "beijing", 
        "c5": "北京", 
        "c6": "beijing", 
        "c7": "北京", 
        "c8": "china", 
        "c9": "中國", 
        "c10": "1", 
        "c11": "010", 
        "c12": "100000", 
        "c13": "116.391", 
        "c14": "39.904", 
        "c15": "33", 
        "c16": "AZ9010", 
        "c17": "+8"
    }, 
    "f": {
        "f1": [
            {
                "fa": "01", 
                "fb": "03", 
                "fc": "10", 
                "fd": "5", 
                "fe": "0", 
                "ff": "0", 
                "fg": "0", 
                "fh": "0", 
                "fi": "06:21|17:40"
            }, 
            {
                "fa": "07", 
                "fb": "07", 
                "fc": "19", 
                "fd": "12", 
                "fe": "0", 
                "ff": "0", 
                "fg": "0", 
                "fh": "0", 
                "fi": "06:22|17:38"
            }, 
            {
                "fa": "02", 
                "fb": "00", 
                "fc": "15", 
                "fd": "5", 
                "fe": "8", 
                "ff": "8", 
                "fg": "3", 
                "fh": "1", 
                "fi": "06:23|17:37"
            }, 
            {
                "fa": "00", 
                "fb": "00", 
                "fc": "16", 
                "fd": "4", 
                "fe": "0", 
                "ff": "0", 
                "fg": "0", 
                "fh": "0", 
                "fi": "06:24|17:35"
            }, 
            {
                "fa": "00", 
                "fb": "00", 
                "fc": "18", 
                "fd": "7", 
                "fe": "0", 
                "ff": "0", 
                "fg": "0", 
                "fh": "0", 
                "fi": "06:25|17:34"
            }, 
            {
                "fa": "00", 
                "fb": "01", 
                "fc": "18", 
                "fd": "8", 
                "fe": "0", 
                "ff": "0", 
                "fg": "0", 
                "fh": "0", 
                "fi": "06:26|17:32"
            }, 
            {
                "fa": "01", 
                "fb": "01", 
                "fc": "16", 
                "fd": "6", 
                "fe": "0", 
                "ff": "0", 
                "fg": "0", 
                "fh": "0", 
                "fi": "06:27|17:31"
            }
        ], 
        "f0": "201310121100"
    }
}

詳細接口分析如下:

//格式說明 
var format={"fa":圖片1,"fb":圖片2,"fc":溫度1,fd:溫度2,fe:風向1,ff:風向2,fg:風力1,fh:風力2,fi:日出日落}; 
//定義天氣類型
var weatherArr={
    "10": "暴雨", 
    "11": "大暴雨", 
    "12": "特大暴雨", 
    "13": "陣雪", 
    "14": "小雪", 
    "15": "中雪", 
    "16": "大雪", 
    "17": "暴雪", 
    "18": "霧", 
    "19": "凍雨", 
    "20": "沙塵暴", 
    "21": "小到中雨", 
    "22": "中到大雨", 
    "23": "大到暴雨", 
    "24": "暴雨到大暴雨", 
    "25": "大暴雨到特大暴雨", 
    "26": "小到中雪", 
    "27": "中到大雪", 
    "28": "大到暴雪", 
    "29": "浮塵", 
    "30": "揚沙", 
    "31": "強沙塵暴", 
    "53": "霾", 
    "99": "", 
    "00": "晴", 
    "01": "多雲", 
    "02": "陰", 
    "03": "陣雨", 
    "04": "雷陣雨", 
    "05": "雷陣雨伴有冰雹", 
    "06": "雨夾雪", 
    "07": "小雨", 
    "08": "中雨", 
    "09": "大雨"
}; 
//定義風向數組 
var fxArr={
    "0": "無持續風向", 
    "1": "東北風", 
    "2": "東風", 
    "3": "東南風", 
    "4": "南風", 
    "5": "西南風", 
    "6": "西風", 
    "7": "西北風", 
    "8": "北風", 
    "9": "旋轉風"
};
//定義風力數組 
var flArr={
    "0": "微風", 
    "1": "3-4級", 
    "2": "4-5級", 
    "3": "5-6級", 
    "4": "6-7級", 
    "5": "7-8級", 
    "6": "8-9級", 
    "7": "9-10級", 
    "8": "10-11級", 
    "9": "11-12級"
};

3)獲取全國所有城市代碼列表

方法一:XML接口根節點: http://flash.weather.com.cn/wmaps/xml/china.xmlXML接口主要作用是遞歸獲取全國幾千個縣以上單位的城市代碼,如:江蘇的XML地址爲:http://flash.weather.com.cn/wmaps/xml/shanghai.xml

蘇州的XML地址爲:http://flash.weather.com.cn/wmaps/xml/jiangsu.xml

上面頁面獲得太倉city code:101190408合成太倉天氣信息地址:http://m.weather.com.cn/data/101190408.html

下面貼一段PHP代碼實現的,通過XML接口根節點遞歸獲得全國幾千個縣以上城市cide code的代碼,供參考(可直接在終端下運行):

方法二:一次性獲取全國+國外主要城市,8763個城市列表信息。URL:http://mobile.weather.com.cn/js/citylist.xml

 

2. 中國天氣SmartWeatherAPI(http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml)

1)SmartWeatherAPI接口(簡稱”SWA”接口)是中國氣象局面向網絡媒體、手機廠商、第三方氣象服務機構等用戶,通過web方式提供數據氣象服務的官方載體。該數據主要包括預警、實況、指數、常規預報(24小時)等數據內容。

2)接口文檔:http://download.weather.com.cn/creative/SmartWeatherAPI_Lite_WebAPI_3.0.1.rar

3)使用須申請,詳見官網http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml

 

3. 和風天氣

1)數據主要包含:實時天氣,3天內天氣預報,生活指數,空氣質量。

訪問流量:4000次/天。

訪問頻率:200次/分鐘。

2)URL:https://free-api.heweather.com/v5/forecast?city=yourcity&key=yourkey

city:城市名稱,city可通過城市中英文名稱、ID、IP和經緯度進行查詢,經緯度查詢格式爲:經度,緯度。例:city=北京,city=beijing,city=CN101010100,city= 60.194.130.1

key:用戶認證key

3)註冊頁面:https://www.heweather.com/products

4)接口文檔:https://www.heweather.com/documents/api/v5

 

4. 心知天氣(免費版只提供地級市數據)

1)包含數據:中國地級城市、天氣實況、天氣預報(3天)、生活指數(基礎)。

訪問頻率限制:400次/小時

2)api詳述:https://www.seniverse.com/doc

3)使用需註冊。

註冊地址:https://www.seniverse.com/signup

 

5. 彩雲天氣

1)數據包含:實時天氣數據(天氣、溫度、溼度、風向、網速、雲量、降雨量、PM2.5、空氣質量指數)。

2)API詳述:http://wiki.swarma.net/index.php/%E5%BD%A9%E4%BA%91%E5%A4%A9%E6%B0%94API/v2

url示例:https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/realtime.json

https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/realtime.jsonp?callback=MYCALLBACK

3)使用需註冊

產品詳單:http://labs.swarma.net/api/caiyun_api_service_price.pdf

註冊頁面:https://www.caiyunapp.com/dev_center/regist.html

 

6. 中央天氣預報

1)url:http://tj.nineton.cn/Heart/index/all
參數如下:
  city:城市碼
  language:固定值 zh-chs
  unit:溫度單位固定值 c。可不填。也可省略該參數
  aqi:固定值 city。可不填。也可省略該參數
  alarm:固定值 1。可不填。也可省略該參數
  key:祕鑰,固定值 78928e706123c1a8f1766f062bc8676b。可不填。也可省略該參數

url 示例:http://tj.nineton.cn/Heart/index/all?city=CHSH000000&language=zh-chs&unit=c&aqi=city&alarm=1&key=78928e706123c1a8f1766f062bc8676b

http://tj.nineton.cn/Heart/index/all?city=CHSH000000&language=&unit=&aqi=&alarm=&key=

http://tj.nineton.cn/Heart/index/all?city=CHSH000000

json 示例:

{
  "status": "OK",
  "weather": [
    {
      "city_name": "佛山",
      "city_id": "CHGD070000",
      "last_update": "2017-02-19T12:15:00+08:00",
      "now": {
        "text": "陰",
        "code": "9",
        "temperature": "21",
        "feels_like": "21",
        "wind_direction": "南",
        "wind_speed": "10.44",
        "wind_scale": "2",
        "humidity": "58",
        "visibility": "13.8",
        "pressure": "1014",
        "pressure_rising": "未知",
        "air_quality": {
          "city": {
            "aqi": "64",
            "pm25": "46",
            "pm10": "74",
            "so2": "9",
            "no2": "28",
            "co": "0.575",
            "o3": "108",
            "last_update": "2017-02-19T12:00:00+08:00",
            "quality": "良"
          },
          "stations": null
        }
      },
      "today": {
        "sunrise": "06:58 AM",
        "sunset": "6:27 PM",
        "suggestion": {
          "dressing": {
            "brief": "單衣類",
            "details": "建議着長袖T恤、襯衫加單褲等服裝。年老體弱者宜着針織長袖襯衫、馬甲和長褲。"
          },
          "uv": {
            "brief": "最弱",
            "details": "屬弱紫外線輻射天氣,無需特別防護。若長期在戶外,建議塗擦SPF在8-12之間的防曬護膚品。"
          },
          "car_washing": {
            "brief": "不適宜",
            "details": "不宜洗車,未來24小時內有雨,如果在此期間洗車,雨水和路上的泥水可能會再次弄髒您的愛車。"
          },
          "travel": {
            "brief": "適宜",
            "details": "天氣較好,溫度適宜,總體來說還是好天氣哦,這樣的天氣適宜旅遊,您可以盡情地享受大自然的風光。"
          },
          "flu": {
            "brief": "易發期",
            "details": "相對今天出現了較大幅度降溫,較易發生感冒,體質較弱的朋友請注意適當防護。"
          },
          "sport": {
            "brief": "比較適宜",
            "details": "陰天,較適宜進行各種戶內外運動。"
          }
        }
      },
      "future": [
        {
          "date": "2017-02-19",
          "day": "週日",
          "text": "陰/小雨",
          "code1": "9",
          "code2": "13",
          "high": "24",
          "low": "18",
          "cop": "",
          "wind": "微風3級"
        },
        {
          "date": "2017-02-20",
          "day": "週一",
          "text": "陰",
          "code1": "9",
          "code2": "9",
          "high": "23",
          "low": "18",
          "cop": "",
          "wind": "微風3級"
        },
        {
          "date": "2017-02-21",
          "day": "週二",
          "text": "陣雨",
          "code1": "10",
          "code2": "10",
          "high": "22",
          "low": "18",
          "cop": "",
          "wind": "微風3級"
        },
        {
          "date": "2017-02-22",
          "day": "週三",
          "text": "小雨",
          "code1": "13",
          "code2": "13",
          "high": "23",
          "low": "13",
          "cop": "",
          "wind": "微風3級"
        },
        {
          "date": "2017-02-23",
          "day": "週四",
          "text": "小雨",
          "code1": "13",
          "code2": "13",
          "high": "20",
          "low": "10",
          "cop": "",
          "wind": "北風4級"
        },
        {
          "date": "2017-02-24",
          "day": "週五",
          "text": "小雨",
          "code1": "13",
          "code2": "13",
          "high": "14",
          "low": "10",
          "cop": "",
          "wind": "北風4級"
        },
        {
          "date": "2017-02-25",
          "day": "週六",
          "text": "小雨",
          "code1": "13",
          "code2": "13",
          "high": "15",
          "low": "10",
          "cop": "",
          "wind": "微風3級"
        },
        {
          "date": "2017-02-26",
          "day": "週日",
          "text": "小雨",
          "code1": "13",
          "code2": "13",
          "high": "15",
          "low": "10",
          "cop": "",
          "wind": "北風3級"
        },
        {
          "date": "2017-02-27",
          "day": "週一",
          "text": "小雨/多雲",
          "code1": "13",
          "code2": "4",
          "high": "21",
          "low": "11",
          "cop": "",
          "wind": "北風3級"
        },
        {
          "date": "2017-02-28",
          "day": "週二",
          "text": "多雲",
          "code1": "4",
          "code2": "4",
          "high": "24",
          "low": "14",
          "cop": "",
          "wind": "北風3級"
        }
      ]
    }
  ]
}

解析:

status:成功時返回 OK
    weather:天氣信息
    city_name:城市名
    city_id:城市 id
    last_update:上次更新時間
    now:現在天氣狀況
        text:天氣狀況
        code:???
        temperature:溫度
        feels_like:體感溫度
        wind_direction:風向
        wind_speed:風速
        wind_scale:風力大小
        humidity:空氣溼度
        visibility:能見度,單位爲 km
        pressure:氣壓,單位爲 hPa
        air_quality:具體空氣質量指數
            aqi:空氣質量指數
            pm25:pm2.5指數
            pm10:pm10指數
            so2:二氧化硫指數
            no2:二氧化氮指數
            co:一氧化碳指數
            o3:臭氧指數
            last_update:上次更新時間
            quality:空氣質量
    today:今日天氣狀況
        sunrise:日出時間
        sunset:日落時間
        suggestion:建議列表
            dressing:穿衣信息
            uv:紫外線建議
            car_washing:洗車信息
            travel:旅遊信息
            flu:流感信息
            sport:運動信息
                brief:建議、說明
                details:具體信息
    future:未來天氣狀況列表
        date:日期
        day:周幾
        text:天氣狀況
        code1:???
        code2:???
        high:當日最高氣溫
        low:當日最低氣溫
        cop:???
        wind:風力信息

2)24小時天氣預報

url:http://tj.nineton.cn/Heart/index/future24h/

拼接參數:
  city:城市
  language:語言
  key:祕鑰,固定值 78928e706123c1a8f1766f062bc8676b。可不填。也可省略該參數

url 示例:http://tj.nineton.cn/Heart/index/future24h/?city=CHSH000000&language=zh-chs&

key=36bdd59658111bc23ff2bf9aaf6e345c

http://tj.nineton.cn/Heart/index/future24h/?city=CHSH000000&language=&key=

http://tj.nineton.cn/Heart/index/future24h/?city=CHSH000000

json示例

{
  "status": "OK",
  "hourly": [
    {
      "text": "多雲",
      "code": "4",
      "temperature": "16",
      "time": "2017-02-19T13:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "18",
      "time": "2017-02-19T14:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "17",
      "time": "2017-02-19T15:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "16",
      "time": "2017-02-19T16:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "16",
      "time": "2017-02-19T17:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "16",
      "time": "2017-02-19T18:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "15",
      "time": "2017-02-19T19:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "15",
      "time": "2017-02-19T20:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "15",
      "time": "2017-02-19T21:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "14",
      "time": "2017-02-19T22:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "14",
      "time": "2017-02-19T23:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "14",
      "time": "2017-02-20T00:00:00+08:00"
    },
    {
      "text": "多雲",
      "code": "4",
      "temperature": "15",
      "time": "2017-02-20T01:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "15",
      "time": "2017-02-20T02:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "15",
      "time": "2017-02-20T03:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "15",
      "time": "2017-02-20T04:00:00+08:00"
    },
    {
      "text": "中雨",
      "code": "14",
      "temperature": "15",
      "time": "2017-02-20T05:00:00+08:00"
    },
    {
      "text": "中雨",
      "code": "14",
      "temperature": "13",
      "time": "2017-02-20T06:00:00+08:00"
    },
    {
      "text": "中雨",
      "code": "14",
      "temperature": "10",
      "time": "2017-02-20T07:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "8",
      "time": "2017-02-20T08:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "6",
      "time": "2017-02-20T09:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "5",
      "time": "2017-02-20T10:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "5",
      "time": "2017-02-20T11:00:00+08:00"
    },
    {
      "text": "小雨",
      "code": "13",
      "temperature": "6",
      "time": "2017-02-20T12:00:00+08:00"
    }
  ]
}

解析

status:成功時返回 OK
hourly:具體小時天氣信息列表
    text:天氣狀況
    code:請參考 [code 細節]
    temperature:溫度
    time:時間

另 ,code細節

/// 晴
case sunny = 0
/// 晴
case clear = 1
/// 晴
case fair1 = 2
/// 晴
case fair2 = 3

/// 多雲
case cloudy = 4
/// 晴間多雲
case partlyCloudy1 = 5
/// 晴間多雲
case partlyCloudy2 = 6
/// 大部多雲
case mostlyCloudy1 = 7
/// 大部多雲
case mostlyCloudy2 = 8

/// 陰
case overcast = 9
/// 陣雨
case shower = 10
/// 雷陣雨
case thundershower = 11
/// 雷陣雨伴有冰雹
case thundershowerWithHail = 12
/// 小雨
case lightRain = 13
/// 中雨
case moderateRain = 14
/// 大雨
case heavyRain = 15
/// 暴雨
case storm = 16
/// 大暴雨
case heavyStorm = 17
/// 特大暴雨
case severeStorm = 18

/// 凍雨
case iceRain = 19
/// 雨夾雪
case sleet = 20
/// 陣雪
case snowFlurry = 21
/// 小雪
case lightSnow = 22
/// 中雪
case moderateSnow = 23
/// 大雪
case heavySnow = 24
/// 暴雪
case snowstorm = 25

/// 浮塵
case dust = 26
/// 揚沙
case sand = 27
/// 沙塵暴
case duststorm = 28
/// 強沙塵暴
case sandstorm = 29
/// 霧
case foggy = 30
/// 霾
case haze = 31
/// 風
case windy = 32
/// 大風
case blustery = 33
/// 颶風
case hurricane = 34
/// 熱帶風暴
case tropicalStorm = 35
/// 龍捲風
case tornado = 36

/// 冷
case cold = 37
/// 熱
case hot = 38

/// 未知
case unknown = 99

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章