I've read numerous threads asking similar questions but have been unable to tie it all together.
I have an API feeding a string here: https://apiv2.bitcoinaverage.com/constants/exchangerates/local
I would like to make this string usable and accessible. For example getting the USD to CAD rate.
I'm using RestSharp and Newtonsoft JSON in my code.
using Newtonsoft.Json;
using RestSharp;
First I used http://json2csharp.com/ to create a class (classes?) matching the string. EDIT: I've now solved this, and had to nest the classes properly, as per revised code;
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; }
}
}
}
I then called the API and stored the response in a string;
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;
I believe there are 1 or 2 steps remaining but I can't quite figure it out. How do I now convert this string to something usable?
Any help is appreciated!