What is JSON?
Why JSON?
JSON Free Web Service Providers
- {
- "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;
- // attempt to download JSON data as a string
- try
- {
- Console.WriteLine("Started downloading data");
- json_data = w.DownloadString(url);
- Console.WriteLine("Completed downloading");
- }
- catch (Exception)
- {}
- // if string with JSON data is not empty, deserialize it to class and return its instance
- 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;
- // attempt to download JSON data as a string
- try
- {
- Console.WriteLine("Started downloading data");
- json_data = w.DownloadString(url);
- Console.WriteLine("Completed downloading");
- }
- catch (Exception)
- {}
- // if string with JSON data is not empty, deserialize it to class and return its instance
- return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();
- }
- }
- }
- }


Pro MelioraPosted Feb 14, 2021, 3:33 PM
God bless you. I’ve hunted all over for this, and until this page have found only information for people who likely don’t need it.
Sridhar SharmaPosted Nov 20, 2015, 9:46 AM
Thanks SanthaKumar :)
Santhakumar MunuswamyPosted Nov 20, 2015, 9:36 AM
Good One
kakarla jayasreePosted Nov 20, 2015, 5:15 AM
Good Information
Humayun Kabir MamunPosted Nov 19, 2015, 1:12 AM
Nice...
Sridhar SharmaPosted Nov 18, 2015, 11:09 PM
Thanks Ershad
Former memberPosted Nov 18, 2015, 1:28 PM
very good
Banketeshvar NarayanPosted Nov 18, 2015, 11:09 AM
Nice Share
Manas MohapatraPosted Nov 18, 2015, 10:43 AM
Good One Sri.. Welcome to C# Corner :)
Raja TPosted Nov 18, 2015, 7:33 AM
Nice, Thanks for sharing
Sibeesh VenuPosted Nov 18, 2015, 7:26 AM
Nice Share
Harshad PansuriyaPosted Nov 18, 2015, 7:25 AM
Nice