0

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

Ithra
  • 45
  • 6
  • 1
    What problems are you having? Please show details, like an example of the JSON produced and the C# classes that you are trying to deserialize into. – Heretic Monkey Oct 05 '20 at 12:16
  • 1
    Please site the examples that you have tried, and the errors you encountered when they did not work. – Simon Bosley Oct 05 '20 at 12:16
  • This is the JSON I get : {"rates":{"EURUSD":{"rate":1.175883,"timestamp":1601901665}},"code":200} And I would like to deserialize the rate, timestamp and the code – Ithra Oct 05 '20 at 13:03

2 Answers2

0

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^

Seriy_A
  • 31
  • 1
  • 8
0

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