【发布时间】:2018-12-31 18:53:38
【问题描述】:
我已经阅读了许多询问类似问题的主题,但无法将它们全部联系在一起。
我有一个 API 在这里输入一个字符串:https://apiv2.bitcoinaverage.com/constants/exchangerates/local
我想让这个字符串可用且可访问。例如获取美元对加元的汇率。
我在我的代码中使用 RestSharp 和 Newtonsoft JSON。
using Newtonsoft.Json;
using RestSharp;
首先我使用http://json2csharp.com/ 创建一个匹配字符串的类(类?)。编辑:我现在已经解决了这个问题,并且必须按照修改后的代码正确嵌套类;
class Exrates
{
public Rates rates { get; set; }
public string time { get; set; }
public class Rates
{
public MXN Mxn { get; set; }
public ILS Ils { get; set; }
public EUR Eur { get; set; }
public BRL Brl { get; set; }
public PLN Pln { get; set; }
public MYR Myr { get; set; }
public SEK Sek { get; set; }
public AUD Aud { get; set; }
public IDR Idr { get; set; }
public TRY Try { get; set; }
public RUB Rub { get; set; }
public JPY Jpy { get; set; }
public CAD Cad { get; set; }
public USD Usd { get; set; }
public GBP Gbp { get; set; }
public NZD Nzd { get; set; }
public CZK Czk { get; set; }
public SGD Sgd { get; set; }
public class MXN
{
public string name { get; set; }
public string rate { get; set; }
}
public class ILS
{
public string name { get; set; }
public string rate { get; set; }
}
public class EUR
{
public string name { get; set; }
public string rate { get; set; }
}
public class BRL
{
public string name { get; set; }
public string rate { get; set; }
}
public class PLN
{
public string name { get; set; }
public string rate { get; set; }
}
public class MYR
{
public string name { get; set; }
public string rate { get; set; }
}
public class SEK
{
public string name { get; set; }
public string rate { get; set; }
}
public class AUD
{
public string name { get; set; }
public string rate { get; set; }
}
public class IDR
{
public string name { get; set; }
public string rate { get; set; }
}
public class TRY
{
public string name { get; set; }
public string rate { get; set; }
}
public class RUB
{
public string name { get; set; }
public string rate { get; set; }
}
public class JPY
{
public string name { get; set; }
public string rate { get; set; }
}
public class CAD
{
public string name { get; set; }
public string rate { get; set; }
}
public class USD
{
public string name { get; set; }
public string rate { get; set; }
}
public class GBP
{
public string name { get; set; }
public string rate { get; set; }
}
public class NZD
{
public string name { get; set; }
public string rate { get; set; }
}
public class CZK
{
public string name { get; set; }
public string rate { get; set; }
}
public class SGD
{
public string name { get; set; }
public string rate { get; set; }
}
}
}
然后我调用 API 并将响应存储在字符串中;
var btcAvgClient = new RestClient();
btcAvgClient.BaseUrl = new Uri("https://apiv2.bitcoinaverage.com/constants/exchangerates/local");
IRestResponse response;
var request = new RestRequest();
response = btcAvgClient.Execute(request);
string btcAvg = response.Content;
我相信还有 1 或 2 个步骤,但我无法完全弄清楚。我现在如何将此字符串转换为可用的东西?
感谢任何帮助!
【问题讨论】: