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.
Look again at rates
in the JSON:
"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 }
}
You've declared that as Dictionary<string, Rate>
, which is fine - but that means you expect something like this:
{ "JPY": 124.51 }
... to be deserialized as a Rate
. You've got one property, Valuta
, with no JsonProperty
attribute, so what do you expect Json.NET to do with the "JPY" property in the JSON?
If you're only ever going to get JPY as the key, you could just specify that on Rate
:
public partial class Rate
{
[JsonProperty("JPY")]
public double Valuta { get; set; }
}
... but I suspect it makes more sense to just declare Rates
as a Dictionary<string, Dictionary<string, double>>
instead (and remove your Rate
class entirely).