A Simple Way To Deserialize JSON

What is JSON?

JSON (JavaScript Object Notation) is a way to store information in an organized, easy-to-access manner.

Why JSON?

JSON provides faster and asynchronous data loading without delaying page rendering.

JSON Free Web Service Providers

Have a look to the site.

In this article, I will show a sample program to read random quantum number from a site that provides JSON Service and after fetching the data, we will de-serialize it into an object.

Firstly, create a console application and install Newtonsoft.JSON using Package Manager Console.

PM> install-package Newtonsoft.Json

I’ll call the link from program to fetch the random number and then we will de-serialize.

When you try to access the above link, you should be able to see the data something like the following:

  1. {  
  2.     "type""uint8",  
  3.     "length": 1,  
  4.     "data": [52],  
  5.     "success"true  
  6. }  
We will create a class as shown below which will be used for holding de-serialized value.
  1. public class QuantumRandomNumber  
  2. {  
  3.     public string type  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string length  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public List < string > data  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string success  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }  
For downloading and de-serializing JSON, we will use the following method:
  1. private static T _download_serialized_json_data < T > (string url) where T: new()  
  2. {  
  3.     using(var w = new System.Net.WebClient())  
  4.     {  
  5.         var json_data = string.Empty;  
  6.         // attempt to download JSON data as a string  
  7.         try  
  8.         {  
  9.             Console.WriteLine("Started downloading data");  
  10.             json_data = w.DownloadString(url);  
  11.             Console.WriteLine("Completed downloading");  
  12.         }  
  13.         catch (Exception)  
  14.         {}  
  15.         // if string with JSON data is not empty, deserialize it to class and return its instance  
  16.         return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();  
  17.     }  
  18. }  
Now, Call the URL
  1. static void Main(string[] args)  
  2. {  
  3.     var url = "https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint8";  
  4.     var t = _download_serialized_json_data < QuantumRandomNumber > (url);  
  5.     Console.WriteLine("Quantum Number details are:");  
  6.     Console.WriteLine("Quantum Number :" + t.data[0].ToString());  
  7.     Console.WriteLine("Quantum Number Type:" + t.type);  
  8.     Console.WriteLine("Quantum Number Length:" + t.length);  
  9.     Console.ReadLine();  
  10. }  
Entire Program
  1. using Newtonsoft.Json;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. namespace QuantumNumber  
  8. {  
  9.     class Program  
  10.     {  
  11.         public class QuantumRandomNumber  
  12.         {  
  13.             public string type  
  14.             {  
  15.                 get;  
  16.                 set;  
  17.             }  
  18.             public string length  
  19.             {  
  20.                 get;  
  21.                 set;  
  22.             }  
  23.             public List < string > data  
  24.             {  
  25.                 get;  
  26.                 set;  
  27.             }  
  28.             public string success  
  29.             {  
  30.                 get;  
  31.                 set;  
  32.             }  
  33.         }  
  34.         static void Main(string[] args)  
  35.         {  
  36.             var url = "https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint8";  
  37.             var t = _download_serialized_json_data < QuantumRandomNumber > (url);  
  38.             Console.WriteLine("Quantum Number details are:");  
  39.             Console.WriteLine("Quantum Number :" + t.data[0].ToString());  
  40.             Console.WriteLine("Quantum Number Type:" + t.type);  
  41.             Console.WriteLine("Quantum Number Length:" + t.length);  
  42.             Console.ReadLine();  
  43.         }  
  44.         private static T _download_serialized_json_data < T > (string url) where T: new()  
  45.         {  
  46.             using(var w = new System.Net.WebClient())  
  47.             {  
  48.                 var json_data = string.Empty;  
  49.                 // attempt to download JSON data as a string  
  50.                 try  
  51.                 {  
  52.                     Console.WriteLine("Started downloading data");  
  53.                     json_data = w.DownloadString(url);  
  54.                     Console.WriteLine("Completed downloading");  
  55.                 }  
  56.                 catch (Exception)  
  57.                 {}  
  58.                 // if string with JSON data is not empty, deserialize it to class and return its instance  
  59.                 return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();  
  60.             }  
  61.         }  
  62.     }  
  63. }  
Output

output

JSON is a faster way to load data. For more details about JSON, click on the link

Up Next
    Ebook Download
    View all
    Learn
    View all