With reference to Parse JSON in C#
I am trying to parse the following JSON feed in C# and I have a problem with accessing the data in "rates". I have tried to deserialize this into a List> or Dictionary and various other types I always get 0 results. The only way I managed to get it work was by creating a custom type and having all the currencies I need as properties - which is quite nice.
Here are my current DataContracts:
[DataContract]
public class OpenExchangeRatesResult
{
public OpenExchangeRatesResult() { }
[DataMember]
public string disclaimer { get; set; }
[DataMember]
public RatesObj rates { get; set; }
}
[DataContract]
public class RatesObj
{
public RatesObj() { }
[DataMember]
public decimal EUR { get; set; }
[DataMember]
public decimal USD { get; set; }
[DataMember]
public decimal GBP { get; set; }
[DataMember]
public decimal AUD { get; set; }
[DataMember]
public decimal CAD { get; set; }
[DataMember]
public decimal CHF { get; set; }
[DataMember]
public decimal DKK { get; set; }
[DataMember]
public decimal LYD { get; set; }
[DataMember]
public decimal NZD { get; set; }
[DataMember]
public decimal SEK { get; set; }
[DataMember]
public decimal JPY { get; set; }
[DataMember]
public decimal ZAR { get; set; }
}
I would love to be able to replace the RatesObj with a collection of some sort.
Any ideas?
I'm using JSON.net (documentation here), which is probably similar to the DataContractJsonSerializer, but I just replaced your
RatesObj
with aDictionary<string,decimal>
:And my test code:
I end up with the rates property having some 150 entries of strings (the currency name) and decimals (the rates). Hopefully this is what you're looking for.
I hope this helps. Let me know if you have other questions and I'll elaborate my answer.
Good luck!