【发布时间】:2020-09-06 08:47:07
【问题描述】:
我正在使用 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 数据,但是当调用该函数时,所有值在调试器中显示为 0 - Breakpoint with debugger,这会导致字符串更新为“0”。
谁能告诉我我做错了什么?
非常感谢。
【问题讨论】:
-
您使用的是哪个版本的 Json.net?库存的 NuGet 实施?还是专门为统一而建造的?另外,什么目标平台? Json.net 默认使用动态代码执行,这在某些平台(如 iOS)上是不允许的。我使用了 Json.Net 的统一特定实现,但没有遇到问题:assetstore.unity.com/packages/tools/input-management/…。如果我是你,我会从那里开始。
-
@Basic 我正在使用 Asset Store 中最新的 JSON.NET For Unity(与您提供的链接相同)。 Unity版本2020.1.4f1,目标平台为Windows,架构为x86_64。问题不在于我可以获得的 API 数据。我假设这是反序列化的问题,因为值显示为 0。
-
好的,保证工作但耗时的选项是从您的项目中删除 json.net dll 并将 json.net 源解压缩到您的项目中。这将允许您进入 json.net 代码。您应该能够通过存在正确的符号 (.pdb) 并在 VS 调试选项中禁用“仅我的代码”来实现相同的目的(但我没有在 Json.Net 中尝试过,所以不能说它随符号)。但是在投入任何时间之前...尝试使用一个双精度和一个很小的硬编码 json 字符串创建一个最小的测试用例,以确保您的测试中没有错误。
-
@Basic 我弄清楚了问题所在。在我使用的 API 中,JSON 输出中的任何
:后面都没有空格,因此它无法在物理上准备好任何数据。我用一些示例 API 进行了测试,只能从:符号后有空格的那些 API 中获取数据,即"userId": 1;vs"username":kathy;或"username":"kathy";对此有什么建议吗? -
@Basic 所以我发现了一个汇率 API,在
:符号后有空格,但这仍然在调试器中返回 0 - openexchangerates.org/api/… 这个 API 实际上在调试器中返回数据 - @987654324 @我还错过了什么吗?
标签: c# unity3d json.net unitywebrequest