使用UnityWebRequest从API端点接收JSON数据不起作用

问题描述

我正在使用Unity开发一款经济模拟游戏,并且需要从API获取实时汇率。为此,我正在使用fixer.io。我已经使用UnityWebRequest实现了它,请参见下面的代码

using Newtonsoft.Json;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class ExchangeRequest : MonoBehavIoUr
{
    private string url = "http://data.fixer.io/api/latest?access_key=**********";

    Text exchange;

    public void GetExchangeRates()
    {
        StartCoroutine(MakeRequest());
    }

    IEnumerator MakeRequest()
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        yield return request.SendWebRequest();

        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
        }
        else
        {
            Debug.Log("Received" + request.downloadHandler.text);
            var Rates = JsonConvert.DeserializeObject<Rates>(request.downloadHandler.text);

            exchange = GameObject.Find("MainText").GetComponent<Text>();
            exchange.text = Rates.ZAR.ToString();
        }
    }
}

以及用于解析JSON

public class Rates
{
    public double AED { get; set; }
    public double AFN { get; set; }
    public double ALL { get; set; }
    public double AMD { get; set; }
    public double ANG { get; set; }
    public double AOA { get; set; }
    public double ARS { get; set; }
    public double AUD { get; set; }
    public double AWG { get; set; }
    public double AZN { get; set; }
    public double BAM { get; set; }
    public double BBD { get; set; }
    public double BDT { get; set; }
    public double BGN { get; set; }
    public double BHD { get; set; }
    public double BIF { get; set; }
    public double BMD { get; set; }
    public double BND { get; set; }
    public double BOB { get; set; }
    public double BRL { get; set; }
    public double BSD { get; set; }
    public double BTC { get; set; }
    public double BTN { get; set; }
    public double BWP { get; set; }
    public double BYN { get; set; }
    public double BYR { get; set; }
    public double BZD { get; set; }
    public double CAD { get; set; }
    public double CDF { get; set; }
    public double CHF { get; set; }
    public double CLF { get; set; }
    public double CLP { get; set; }
    public double CNY { get; set; }
    public double cop { get; set; }
    public double CRC { get; set; }
    public double CUC { get; set; }
    public double CUP { get; set; }
    public double CVE { get; set; }
    public double CZK { get; set; }
    public double DJF { get; set; }
    public double DKK { get; set; }
    public double DOP { get; set; }
    public double DZD { get; set; }
    public double EGP { get; set; }
    public double ERN { get; set; }
    public double ETB { get; set; }
    public int EUR { get; set; }
    public double FJD { get; set; }
    public double FKP { get; set; }
    public double GBP { get; set; }
    public double GEL { get; set; }
    public double GGP { get; set; }
    public double GHS { get; set; }
    public double GIP { get; set; }
    public double GMD { get; set; }
    public double GNF { get; set; }
    public double GTQ { get; set; }
    public double GYD { get; set; }
    public double HKD { get; set; }
    public double HNL { get; set; }
    public double HRK { get; set; }
    public double HTG { get; set; }
    public double HUF { get; set; }
    public double IDR { get; set; }
    public double ILS { get; set; }
    public double IMP { get; set; }
    public double INR { get; set; }
    public double IQD { get; set; }
    public double IRR { get; set; }
    public double ISK { get; set; }
    public double JEP { get; set; }
    public double JMD { get; set; }
    public double JOD { get; set; }
    public double JPY { get; set; }
    public double KES { get; set; }
    public double KGS { get; set; }
    public double KHR { get; set; }
    public double KMF { get; set; }
    public double KPW { get; set; }
    public double KRW { get; set; }
    public double KWD { get; set; }
    public double KYD { get; set; }
    public double KZT { get; set; }
    public double LAK { get; set; }
    public double LBP { get; set; }
    public double LKR { get; set; }
    public double LRD { get; set; }
    public double LSL { get; set; }
    public double LTL { get; set; }
    public double LVL { get; set; }
    public double LYD { get; set; }
    public double MAD { get; set; }
    public double MDL { get; set; }
    public double MGA { get; set; }
    public double MKD { get; set; }
    public double MMK { get; set; }
    public double MNT { get; set; }
    public double MOP { get; set; }
    public double MRO { get; set; }
    public double MUR { get; set; }
    public double MVR { get; set; }
    public double MWK { get; set; }
    public double MXN { get; set; }
    public double MYR { get; set; }
    public double MZN { get; set; }
    public double NAD { get; set; }
    public double NGN { get; set; }
    public double NIO { get; set; }
    public double NOK { get; set; }
    public double NPR { get; set; }
    public double NZD { get; set; }
    public double omr { get; set; }
    public double PAB { get; set; }
    public double PEN { get; set; }
    public double PGK { get; set; }
    public double PHP { get; set; }
    public double PKR { get; set; }
    public double PLN { get; set; }
    public double PYG { get; set; }
    public double QAR { get; set; }
    public double RON { get; set; }
    public double RSD { get; set; }
    public double RUB { get; set; }
    public double RWF { get; set; }
    public double SAR { get; set; }
    public double SBD { get; set; }
    public double SCR { get; set; }
    public double SDG { get; set; }
    public double SEK { get; set; }
    public double SGD { get; set; }
    public double SHP { get; set; }
    public double sll { get; set; }
    public double SOS { get; set; }
    public double SRD { get; set; }
    public double STD { get; set; }
    public double SVC { get; set; }
    public double SYP { get; set; }
    public double SZL { get; set; }
    public double THB { get; set; }
    public double TJS { get; set; }
    public double TMT { get; set; }
    public double TND { get; set; }
    public double TOP { get; set; }
    public double TRY { get; set; }
    public double TTD { get; set; }
    public double TWD { get; set; }
    public double TZS { get; set; }
    public double UAH { get; set; }
    public double UGX { get; set; }
    public double USD { get; set; }
    public double UYU { get; set; }
    public double UZS { get; set; }
    public double VEF { get; set; }
    public double VND { get; set; }
    public double VUV { get; set; }
    public double WST { get; set; }
    public double XAF { get; set; }
    public double XAG { get; set; }
    public double XAU { get; set; }
    public double XCD { get; set; }
    public double XDR { get; set; }
    public double XOF { get; set; }
    public double XPF { get; set; }
    public double YER { get; set; }
    public double ZAR { get; set; }
    public double ZMK { get; set; }
    public double ZMW { get; set; }
    public double ZWL { get; set; }
}

public class ExchangeRates
{
    public bool success { get; set; }
    public int timestamp { get; set; }
    public string date { get; set; }
    public double rates { get; set; }
}

Debug.Log("Received" + request.downloadHandler.text);返回API数据,但是在调用函数时,所有值在调试器-Breakpoint with debugger显示为0,这将导致字符串更新为“ 0”。 / p>

任何人都可以告诉我我可能做错了什么吗?

非常感谢。

解决方法

所以实际上真的很简单。

我试图获取的数据是JSON输出中的列表,这就是为什么初始方法不起作用的原因。

{
  "disclaimer": "Usage subject to terms: https://openexchangerates.org/terms","license": "https://openexchangerates.org/license","timestamp": 1599411203,"base": "USD","rates": {
    "AUD": 1.372721,"EUR": 0.844691,"GBP": 0.752474,"JPY": 106.24548164,"USD": 1,"ZAR": 16.58301
  }
}

我更新了JSON解析器,以在JSON输出中包括“比率”列表的类:

public class ExchangeRates
{
    public Rates rates { get; set; }
}

public class Rates
{
    public double AUD { get; set; }
    public double EUR { get; set; }
    public double GBP { get; set; }
    public double JPY { get; set; }
    public double USD { get; set; }
    public double ZAR { get; set; }
}

然后我更新了我的函数,以反序列化可以解析各个费率的列表:

var xr = JsonConvert.DeserializeObject<ExchangeRates>(request.downloadHandler.text);
exchange = GameObject.Find("MainText").GetComponent<Text>();
exchange.text = xr.rates.ZAR.ToString();

现在我可以获取JSON数据了。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...