Unity反序列天气API的JSON
心知天气:https://www.seniverse.com/
JSON:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | { "results" : [ { "location" : { "id" : "C23NB62W20TF" , "name" : "西雅图" , "country" : "US" , "path" : "西雅图,华盛顿州,美国" , "timezone" : "America/Los_Angeles" , "timezone_offset" : "-07:00" }, "now" : { "text" : "多云" , "code" : "4" , "temperature" : "14" , "feels_like" : "14" , "pressure" : "1018" , "humidity" : "76" , "visibility" : "16.09" , "wind_direction" : "西北" , "wind_direction_degree" : "340" , "wind_speed" : "8.05" , "wind_scale" : "2" , "clouds" : "90" , "dew_point" : "-12" }, "last_update" : "2015-09-25T22:45:00-07:00" } ] } |
反序列:
using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class listres { public List<ResponseData> results = new List<ResponseData>(); } [System.Serializable] public class ResponseData { public locationdata location; public nowdata now; public string last_update; } [System.Serializable] public class locationdata { public string id; public string name; public string country; public string path; public string timezone; public string timezone_offset; } [System.Serializable] public class nowdata { public string text; public int code; public int temperature; public int feels_like; public int pressure; public int humidity; public float visibility; public string wind_direction; public int wind_direction_degree; public float wind_speed; public int wind_scale; public int clouds; public string dew_point; } public class WeatherQuerier : MonoBehaviour { // Start is called before the first frame update IEnumerator Start() { WWW www=new WWW("https://api.seniverse.com/v3/weather/now.json?key=SGRa_X2yE0sr74OOd&location=beijing&language=zh-Hans&unit=c"); yield return www; print(www.text); listres respon =JsonUtility.FromJson<listres>(www.text); print(respon.results[0].location.path); } // Update is called once per frame void Update() { } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)