keyvalue pair value is 0 when reading a json file

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
    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).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top