Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use json object in c#

http://xurrency.com/api this webservice is returning a json response message. how can I use this message as an object in my .net project (asp.net web app)

like image 773
Bilgin Kılıç Avatar asked Feb 13 '26 07:02

Bilgin Kılıç


1 Answers

You could start by defining the model classes that will handle the response:

public class XurrencyResponse
{
    public string Status { get; set; }
    public string Code { get; set; }
    public string Message { get; set; }
    public Result Result { get; set; }
}

public class Result
{
    public decimal Value { get; set; }
    public string Target { get; set; }
    public string Base { get; set; }
    public DateTime Updated_At { get; set; }
}

Once you have them you simply call the service:

class Program
{
    static void Main()
    {
        var serializer = new JavaScriptSerializer();
        string json = null;
        using (var client = new WebClient())
        {
            json = client.DownloadString("http://xurrency.com/api/eur/gbp/1.5");
        }
        var response = serializer.Deserialize<XurrencyResponse>(json);
        Console.WriteLine(response.Status);
    }
}
like image 158
Darin Dimitrov Avatar answered Feb 14 '26 21:02

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!