前提
unityで今日の天気を取得するシステムを作っています。
レスポンスが返ってきたことは確認できたのですが、jsonの中身を参照する方法がよくわかりませんでした。
実現したいこと
https://weather.tsukumijima.net/api/forecast?city=400040
このjsonのうち天気(response.forecasts[0].detail.weather)を表示したいです。
該当のソースコード
unit
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.Networking; 5using UnityEngine.UI; 6 7 8public class WeatherController : MonoBehaviour 9{ 10 static string access_url = "https://weather.tsukumijima.net/api/forecast?city=400000"; 11 // Start is called before the first frame update 12 void Start() 13 { 14 //StartCoroutine("ApiRequest"); 15 StartCoroutine(ApiRequest()); 16 } 17 18 // Update is called once per frame 19 void Update() 20 { 21 22 } 23 static IEnumerator ApiRequest()//非同期通信 IEnumerator 24 { 25 UnityWebRequest request = UnityWebRequest.Get(access_url); 26 request.SetRequestHeader("Content-Type", "application/json"); 27 28 yield return request.SendWebRequest(); 29 30 if (request.isHttpError) 31 { 32 // レスポンスコードを見て処理 33 Debug.Log($"[Error]Response Code : {request.responseCode}"); 34 } 35 else if (request.isNetworkError) 36 { 37 // エラーメッセージを見て処理 38 Debug.Log($"[Error]Message : {request.error}"); 39 } 40 else 41 { 42 // 成功したときの処理 43 Response response = JsonUtility.FromJson<Response>(request.downloadHandler.text); 44 45 //全文は表示される 46 Debug.Log(request.downloadHandler.text); 47 //天気(response.forecasts[0].detail.weather)を表示したい 48 Debug.Log("weather: " + response.forecasts[0].detail.weather); 49 50 yield return null; 51 } 52 53 } 54 [System.Serializable] 55 public class Response 56 { 57 public IList<string> forecasts { get; set; } 58 public string detail{ get; set; } 59 public string weather { get; set; } 60 61 } 62 63 64} 65
発生している問題・エラーメッセージ
//全文は表示されるけど Debug.Log(request.downloadHandler.text);
//天気が表示されない //ひとまずforecastsを表示してみる Debug.Log("weather: " + response.forecasts);
//これだとエラー Debug.Log("weather: " + response.forecasts[0].detail.weather);
試したこと
当該jsonに対して他webサイトを見様見真似で様々なアプローチをしたが取り出せなかった。
補足情報(FW/ツールのバージョンなど)
unity version 2021.3.3f1
天気API参照:https://weather.tsukumijima.net/
ご回答宜しくお願い致します。
修正後
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class WeatherController : MonoBehaviour { static string access_url = "https://weather.tsukumijima.net/api/forecast?city=400040"; // Start is called before the first frame update void Start() { //StartCoroutine("ApiRequest"); StartCoroutine(ApiRequest()); } // Update is called once per frame void Update() { } static IEnumerator ApiRequest()//非同期通信 IEnumerator { UnityWebRequest request = UnityWebRequest.Get(access_url); request.SetRequestHeader("Content-Type", "application/json"); yield return request.SendWebRequest(); if (request.isHttpError) { // レスポンスコードを見て処理 Debug.Log($"[Error]Response Code : {request.responseCode}"); } else if (request.isNetworkError) { // エラーメッセージを見て処理 Debug.Log($"[Error]Message : {request.error}"); } else { // 成功したときの処理 Weather response = JsonUtility.FromJson<Weather>(request.downloadHandler.text); //全文は表示されるけど Debug.Log(request.downloadHandler.text); //天気を表示したい //Debug.Log("weather: " + response.forecasts[0].detail.weather); Debug.Log("weather: " + response.weather); yield return null; } } public class Forecasts { public IList <string> forecasts { get; set; } } public class Detail:Forecasts { public string detail { get; set; } } public class Weather:Detail { public string weather { get; set; } } }
回答2件
あなたの回答
tips
プレビュー