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:
- {
- "type": "uint8",
- "length": 1,
- "data": [52],
- "success": true
- }
We will create a class as shown below which will be used for holding de-serialized value.
- public class QuantumRandomNumber
- {
- public string type
- {
- get;
- set;
- }
- public string length
- {
- get;
- set;
- }
- public List < string > data
- {
- get;
- set;
- }
- public string success
- {
- get;
- set;
- }
- }
For downloading and de-serializing JSON, we will use the following method:
- private static T _download_serialized_json_data < T > (string url) where T: new()
- {
- using(var w = new System.Net.WebClient())
- {
- var json_data = string.Empty;
-
- try
- {
- Console.WriteLine("Started downloading data");
- json_data = w.DownloadString(url);
- Console.WriteLine("Completed downloading");
- }
- catch (Exception)
- {}
-
- return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();
- }
- }
Now, Call the URL - static void Main(string[] args)
- {
- var url = "https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint8";
- var t = _download_serialized_json_data < QuantumRandomNumber > (url);
- Console.WriteLine("Quantum Number details are:");
- Console.WriteLine("Quantum Number :" + t.data[0].ToString());
- Console.WriteLine("Quantum Number Type:" + t.type);
- Console.WriteLine("Quantum Number Length:" + t.length);
- Console.ReadLine();
- }
Entire Program - using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace QuantumNumber
- {
- class Program
- {
- public class QuantumRandomNumber
- {
- public string type
- {
- get;
- set;
- }
- public string length
- {
- get;
- set;
- }
- public List < string > data
- {
- get;
- set;
- }
- public string success
- {
- get;
- set;
- }
- }
- static void Main(string[] args)
- {
- var url = "https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint8";
- var t = _download_serialized_json_data < QuantumRandomNumber > (url);
- Console.WriteLine("Quantum Number details are:");
- Console.WriteLine("Quantum Number :" + t.data[0].ToString());
- Console.WriteLine("Quantum Number Type:" + t.type);
- Console.WriteLine("Quantum Number Length:" + t.length);
- Console.ReadLine();
- }
- private static T _download_serialized_json_data < T > (string url) where T: new()
- {
- using(var w = new System.Net.WebClient())
- {
- var json_data = string.Empty;
-
- try
- {
- Console.WriteLine("Started downloading data");
- json_data = w.DownloadString(url);
- Console.WriteLine("Completed downloading");
- }
- catch (Exception)
- {}
-
- return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();
- }
- }
- }
- }
Output
JSON is a faster way to load data. For more details about JSON, click on the
link.