简体   繁体   English

将来自 API 的 JSON URL 解析为 Unity/C# 中的可读文本

[英]Parsing JSON URL from API into readable text in Unity/C#

I'm trying to format JSON data from an API and store the results.我正在尝试从 API 格式化 JSON 数据并存储结果。

The data consists of the local weather and provides accurate temperatures based on the current time.数据包含当地天气,并根据当前时间提供准确的温度。

Below is the C# script of what I've tried so far:以下是我迄今为止尝试过的 C# 脚本:

jsonDataController.cs jsonDataController.cs

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

public class jsonDataController : MonoBehaviour
{
    public string jsonURL = "http://api.weatherapi.com/v1/current.json?key=0ee485dfa1af4123a1a103953221107&q=London&aqi=no";

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

    // Update is called once per frame
    void Update()
    {
        
    }

    IEnumerator generateData()
    {
        WWW www = new WWW(jsonURL);
        yield return www;
        if(www.error == null)
        {
            processJsonData(www.text)
        }
    }

    protected void processJsonData(string url)
    {
        jsonDataClass jsonData = JsonUtility.FromJson<jsonDataClass>(url);
        Debug.Log(jsonData.local);
        Debug.Log(jsonData.current);
    }
}

jsonDataClass.cs jsonDataClass.cs

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

[Serializable]
public class jsonDataClass
{
    public string location; 
    public List<locationClass> local;
    public List<currentClass> current;

}

[Serializable]
public class locationClass
{
    public string name;
    public string region;
    public string country;
    public float lat;
    public float lon;
    public string tz_id;
    public int localtime_e;
    public string localtime;

}

[Serializable]
public class currentClass
{
    public float temp_c;
    public float temp_f;
    public string last_updated;

}


And this is my JSON data:这是我的 JSON 数据:

{
  "location": {
    "name": "London",
    "region": "City of London, Greater London",
    "country": "United Kingdom",
    "lat": 51.52,
    "lon": -0.11,
    "tz_id": "Europe/London",
    "localtime_epoch": 1657576622,
    "localtime": "2022-07-11 22:57"
  },
  "current": {
    "last_updated_epoch": 1657575900,
    "last_updated": "2022-07-11 22:45",
    "temp_c": 24,
    "temp_f": 75.2,
    "is_day": 0,
    "condition": {
      "text": "Clear",
      "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png",
      "code": 1000
    },
    "wind_mph": 5.6,
    "wind_kph": 9,
    "wind_degree": 220,
    "wind_dir": "SW",
    "pressure_mb": 1023,
    "pressure_in": 30.21,
    "precip_mm": 0,
    "precip_in": 0,
    "humidity": 44,
    "cloud": 0,
    "feelslike_c": 24.8,
    "feelslike_f": 76.6,
    "vis_km": 10,
    "vis_miles": 6,
    "uv": 6,
    "gust_mph": 6,
    "gust_kph": 9.7
  }
}

Any help would be appreciated.任何帮助,将不胜感激。

you have to fix classes你必须修课

[Serializable]
public class jsonDataClass
{
    public locationClass location;
    public currentClass current;
}


[Serializable]
public class conditionClass
    {
        public string text ;
        public string icon ;
        public int code ;
    }

[Serializable]
public class currentClass
{
    public float temp_c;
    public float temp_f;
    public string last_updated;
   public conditionClass condition;
}
 // add all another fields starting from "wind_mph" to currentClass too

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM