Kim Karlsson
Kim Karlsson

Reputation: 23

Deserializing weirdly formatted JSON to an object in C#

I'm trying to teach myself working with APIs, and chose to try and make something that fetches currency exchange rates from a certain free API regarding just that. I've managed to successfully make the API call and record the JSON data from it, and I'd like to save the data so it's easily accessible from an object.

My issue is this; the JSON data reads like

{
    "Realtime Currency Exchange Rate": {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "EUR",
        "4. To_Currency Name": "Euro",
        "5. Exchange Rate": "0.87490000",
        "6. Last Refreshed": "2020-07-19 08:16:36",
        "7. Time Zone": "UTC",
        "8. Bid Price": "0.87490000",
        "9. Ask Price": "0.87540000"
    }
}

Using https://json2csharp.com/ I'm getting a result like this

public class RealtimeCurrencyExchangeRate    {
    public string 1.From_CurrencyCode { get; set; } 
    public string 2.From_CurrencyName { get; set; } 
    public string 3.To_CurrencyCode { get; set; } 
    public string 4.To_CurrencyName { get; set; } 
    public string 5.ExchangeRate { get; set; } 
    public string 6.LastRefreshed { get; set; } 
    public string 7.TimeZone { get; set; } 
    public string 8.BidPrice { get; set; } 
    public string 9.AskPrice { get; set; } 
}

Which isn't valid c# syntax. The JSON parser I have could deserialize that info (I think) but Visual Studio is throwing a well deserved fit over me trying to name a variable 1.From_CurrencyCode

Upvotes: 2

Views: 112

Answers (2)

andrei.ciprian
andrei.ciprian

Reputation: 3025

I haven't moved to System.Text.Json either, I am sticking with Newtonsoft.Json. At the moment I prefer a convention based approach to deserialization, in this case I wouldn't pollute my model with json attributes. Furthermore, this looks like a bad api to consume in the first place (imho), those property names are awful.

So, in this case, I'd choose a tolerant reader (like Martin Fowler advertises) over strongly typed objects, and one of the 3 techniques below(exclusive OR):

Upvotes: 0

haldo
haldo

Reputation: 16701

You can use the JsonProperty attribute (if you're using Newtonsoft.Json) to decorate each of your model properties. This allows you to rename the Json property names to valid C# property names.

public class RealtimeCurrencyExchangeRate    
{
    [JsonProperty("1. From_Currency Code")]
    public string FromCurrencyCode { get; set; } 
    
    // similarly for the remaining properties
    ...
}

// also apply it to your "root" class
public class Root    
{
    [JsonProperty("Realtime Currency Exchange Rate")]
    public RealtimeCurrencyExchangeRate RealtimeCurrencyExchangeRate { get; set; }
}

Upvotes: 2

Related Questions