JSON TO C# Deserializing

55 views Asked by At

Can someone help me deserialize JSON from this api https://www.freeforexapi.com/api/live?pairs=EURUSD,GBPUSD to a C# Object? I have tried many ways and examples I found online, non seems to be working

2

There are 2 answers

0
Rajan Pipaliya On BEST ANSWER

Here rates is Map, So Response C# class look like this:

public class Response
{
    public Dictionary<string, Rate> Rates{get;set;}
    public int Code {get;set;} 
}

public class Rate
{
    public double Rate { get; set; }
    public long TimeStamp { get; set; }

}

To deserialize :

var obj = JsonConvert.DeserializeObject<Response>(jsonObject);
0
Seriy_A On

for example, using newtonsoft json create class for response

public class EURUSD    {
    public double rate { get; set; } 
    public int timestamp { get; set; } 
}

public class GBPUSD    {
    public double rate { get; set; } 
    public int timestamp { get; set; } 
}

public class Rates    {
    public EURUSD EURUSD { get; set; } 
    public GBPUSD GBPUSD { get; set; } 
}

public class Root    {
    public Rates rates { get; set; } 
    public int code { get; set; } 
}

and then deserialize response, like

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 

you may use json2csharp website to easy generate classes like this^