unity天気インタフェースによるjsonデータの取得

3750 ワード

まず、私が発見した注意点をお話しします.jsonが受信するときのクラスの属性名は必ずjsonのキー値の名前と同じでなければなりません.そうしないと受信できません.この初心者は必ず覚えておいてください.
まず天気インタフェースのapiを探して、ネット上の中国の天気ネットワークの無料インタフェースを探し始めましたが、取得したデータは正確ではありません.その後、簡単な、和風の天気を探して、申請は簡単でも無料です.だから今使っているのは和風です.https://www.heweather.com/
json元のデータ:
 
{"HeWeather6":[{"basic":{"cid":"CN101090101","location":"   ","parent_city":"   ","admin_area":"  ","cnty":"  ","lat":"38.04547501","lon":"114.50246429","tz":"+8.0"},"update":{"loc":"2018-01-04 14:50","utc":"2018-01-04 06:50"},"status":"ok","now":{"cloud":"0","cond_code":"104","cond_txt":" ","fl":"-6","hum":"32","pcpn":"0.0","pres":"1031","tmp":"0","vis":"8","wind_deg":"7","wind_dir":"  ","wind_sc":"  ","wind_spd":"5"}}]}

 
 
 
サンプルコード:
//////////天気取得////private void GetWeather(){StartCoroutine(IEGetWeather()    }     IEnumerator IEGetWeather()     {         if (WeatherURL != null)         {             WWW www = new WWW(WeatherURL);             yield return new WaitUntil(() => www.isDone);             WeatherInfo weather = JsonUtility.FromJson(www.text);             if (m_TempTxt != null)             {                 m_TempTxt.text = weather.HeWeather6[0].now.tmp+"℃";             }             if (m_WeatherIcon != null)             {                 m_WeatherIcon.sprite = Resources.Load("Weather/"+ weather.HeWeather6[0].now.cond_txt);             }            //Debug.Log(weather.weatherinfo.city); }}#region天気Json[Serializable]public class WeatherInfo{public HeWeather 6[]HeWeather 6;    }     [Serializable]     public class HeWeather6     {         public MBase basc;         public MUpdate update;         public string status;         public MNow now;     }     [Serializable]     public class MBase     {         public string cid;         public string location;         public string parent_city;         public string admin_area;         public string cnty;         public string lat;         public string lon;         public string tz;     }     [Serializable]     public class MUpdate     {         public string loc;         public string utc;     }     [Serializable]     public class MNow     {         public string cloud;         public string cond_code;         public string cond_txt;         public string fl;         public string hum;         public string pcpn;         public string pres;         public string tmp;         public string vis;         public string wind_deg;         public string wind_dir;         public string wind_sc;         public string wind_spd;     }     #endregion
 
-----------------------------------------------2018.09.04----------------------------------------------------------------------------------------------------------
新規shi汎用を使用してjsonを取得
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                if (readdata.Length > 0)                 {                     T data = JsonUtility.FromJson(readdata);
                    return data;                 }             }         }
        return default(T);
             }