6

I am trying to consume an API.

I want to store following Request in an Object: http://api.swissunihockey.ch/rest/v1.0/clubs/655
The Problem is, that the Object is initialized but all the values are null.

I can receive the data and generate an output as a string. But the De-serialization to the Object doesn't work. Can you help?

private static async Task RunAsync()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://api.swissunihockey.ch/rest/v1.0/clubs/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        try
        {
            HttpResponseMessage response = await client.GetAsync("615");
            var club = await response.Content.ReadAsAsync<Club>();
            Console.WriteLine(club.Name);
            Console.Read();
        }
        catch (HttpRequestException e)
        {
            if (e.Source != null)
            {
                Console.WriteLine("HttpRequestException source: {0}", e.Source);
            }
        }
    }
}

This is the Club class I am trying to store the data:

class Club
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Street { get; set; }
    public string Zip { get; set; }
    public string City { get; set; }
    public string Canton { get; set; }
    public string Phone { get; set; }
    public string Url { get; set; }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
RobinXSI
  • 933
  • 1
  • 7
  • 18

3 Answers3

3

You need another class containing Club which will be deserialized further.

class Response
{
    public Club Club { get; set; }
}

Then deserialize as

var res = await response.Content.ReadAsAsync<Response>();
var club = res.Club;
Abhishek Chokra
  • 1,381
  • 2
  • 9
  • 17
Loki
  • 286
  • 2
  • 7
3

Once you have the string from the response, use the package that Web API already references from Nuget, Json.Net by Newtonsoft.Json1, and call Club c = JsonConvert.Deserialize<Club>(responseString);

I find this to be far simpler than the built in Data Contracts already mentioned.

Adam Venezia
  • 2,661
  • 2
  • 16
  • 10
  • Actually a combination of DRK answer and your line of code `Club c = JsonConvert.Deserialize(responseString);` did the trick for me. – DanielV Oct 25 '16 at 16:13
0

Try to look on the microsoft related documentation:

http://msdn.microsoft.com/en-us/library/hh674188.aspx

You need to create a data contract and then process the request with this contract.

For example for your, the data contract may be something like:

    [DataContract]
    class Club
    {
        [DataMember(Name = "Id")]
        public int Id { get; set; }
        [DataMember(Name = "Name")]
        public string Name { get; set; }
        [DataMember(Name = "Street")]        
        public string Street { get; set; }
        [DataMember(Name = "Zip")]
        public string Zip { get; set; }
        [DataMember(Name = "City")]
        public string City { get; set; }
        [DataMember(Name = "Canton")]
        public string Canton { get; set; }
        [DataMember(Name = "Phone")]
        public string Phone { get; set; }
        [DataMember(Name = "Url")]
        public string Url { get; set; }
    }

Then look at chapter "process the request" of the documentation to handle your data.

DRK
  • 324
  • 4
  • 15
  • 3
    You don't need to create a Data contract for using REST api. In fact the Data contract breaks the RESTful philosophy of having loose server an client. – diegosasw Sep 30 '14 at 01:12
  • Wouldn't the "loose server and client" come into play when the service is consumed? – user609926 Nov 03 '17 at 19:12