Udemy

Deserializing JSON returned by Restful Service in C#

Saturday, September 27, 2014 0 Comments A+ a-

Nowa days Restful services are more used which mostly return xml or json as reponse. The advantage of restful services are platform independence, a service made restful can be accessed from any platform like Java, c#,php, ruby etc.


I have a service  at this url : http://www.lyfechannel.com/channels/allscripts/allscripts_institutions.php?allscripts=XXXX&user=YYYY&password=ZZZZ  which returns following json as response:


{
"connections":"72", 
"incomplete":"319", 
"qualified":"14", 
"loggedin":"Dr. Frederick Banting | One Medical | San Francisco, Embarcadero", 
"red": "40", 
"yellow": "12", 
"green": "20"
}

First of all we need to create a C# class for deserializing json  to that spcific type, the current json object is not very complex so we can easily write class against this json with properties but mostly we will not have json simple like this, so most of the time i use json2csharp.com to generate classes against the json returned by service.


Our class for this json will look like this:


public class JSONWrapper
{
   public string connections { get; set; }
   public string incomplete { get; set; }
   public string qualified { get; set; }
   public string loggedin { get; set; }
   public string red { get; set; }
   public string yellow { get; set; }
   public string green { get; set; }
}

For deserializing json we have a library available Newtonsoft JSON which we will use for deserializing json to specific type.

We can install it in our project by going to Nuget Package Manager Console in Tools of Visual Studio and writing this command on Nuget Package Manager Console:

PM> Install-Package Newtonsoft.Json 

 If you have dll downloaded on your system you can also add reference by going in to References and click Add Reference from context menu:


 After clicking Add Reference browse to the location where the dll is present and check it and click ok.



Now we will write the code to call the specific url and we will deserialize the returned response to the specific class.


Here we go:


using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;

class Program
{
   static void Main(string[] args)
   {
      try
      {
          string url = "http://www.lyfechannel.com/channels/allscripts/allscripts_institutions.php?allscripts=XXXX&user=YYYY&password=ZZZZ";

          WebRequest request = WebRequest.Create(url);

          request.Timeout = 500000;

          WebResponse response = request.GetResponse();

          var reader = new StreamReader(response.GetResponseStream());

          string json = reader.ReadToEnd();

          var jsonAsCSharp = JsonConvert.DeserializeObject<JSONWrapper>(json);

          Console.WriteLine(jsonAsCSharp);
       }

      catch (Exception e)
      {
         Console.WriteLine("Error Occured:"+e.Message);
      }
   }
}


Here is the  attached sample project console application

Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more