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()
    {
        
    }
}
复制代码

 

posted @   resucase  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示