I'm trying to learn JSON and webrequests in c# and got stuck with this problem:
The keyvaluepair has always the value 0.
JSON (from this api):
"rates": {
"2020-08-03": { "JPY": 124.51 },
"2020-08-25": { "JPY": 125.67 },
"2019-11-20": { "JPY": 119.96 },
"2020-04-08": { "JPY": 118.36 }
},
"start_at": "2019-10-28",
"base": "EUR",
"end_at": "2020-10-28"
}
Conversion.cs
public partial class ConversionRatesLastYear
{
[JsonProperty("rates")]
public Dictionary<string, Rate> Rates { get; set; }
[JsonProperty("start_at")]
public DateTimeOffset StartAt { get; set; }
[JsonProperty("base")]
public string Base { get; set; }
[JsonProperty("end_at")]
public DateTimeOffset EndAt { get; set; }
}
public partial class Rate
{
public double Valuta { get; set; }
}
my method
public void SelectedValuta(string valuta)
{
IRestClient restClient = new RestClient();
DateTime dtLastYear = DateTime.Today.AddYears(-1);
DateTime dtToday = DateTime.Today;
string lastYear = dtLastYear.ToString("yyyy-MM-dd");
string today = dtToday.ToString("yyyy-MM-dd");
Console.WriteLine(lastYear + " " + today);
IRestRequest request = new RestRequest(url + "history?start_at=" + lastYear + "&end_at=" + today + "&symbols="+valuta, DataFormat.Json);
var response = restClient.Get<List<ConversionRatesLastYear>>(request);
foreach (var kvp in response.Data[0].Rates)
{
//gives Value = EuroDollarConverterRealtime.Rate
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
//gives Value = 0
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, (double)kvp.Value.Valuta);
}
}
I can see the entire content from the JSON when I do:
Console.WriteLinke(response.Content)
So the problem isn't getting the information. I can use the "start_at", "base", "end_at" without problem in an another method, but not with "rates". When I do rates I'm only able to get the date and depending if I use kvp.Value I get EuroDollarConverterRealtime.Rate or when I do kvp.Value.Valuta I get 0.
My goal is to get the double value from JPY for each date. I'm fine with it if I'm only able to use the date and the double value from JPY. Note: JPY is an example, this could be every currency except EUR.
Sorry if I'm not able to get every element by it's name, I'm still learning.