How To Work With JSON In ASP.NET

Introduction

 
Pretty excited while writing this article as it’s one of the favorite topics for me. Before jumping into working with JSON, let’s first understand what it is. 
 
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999.
 

What is JSON?

  1. JSON stands for JavaScript Object Notation
  2. JSON is a format to exchange & store data across platforms.
  3. JSON is the best alternative to XML
  4. JSON stores values in the key/value pair format.
work with JSON in ASP.NET
 

How to work with JSON in ASP.NET?

 
With the introduction of JSON, many developers prefer to use JSON instead of XML
 
Below are the reasons for using JSON:
  • XML is expressed in more words than JSON. Hence, JSON is faster to write for programmers.
  • JSON is lightweight and easy to write compared to XML.
  • We can store values as key/value pair in JSON. Whereas, in XML, we have to store values between opening and closing tags.

Working with JSON in ASP.NET

 
JSON follows syntax based on JavaScript and it cannot be processed the same way by C#.
 
In order to work with JSON in ASP.NET or C#, there are three methods.
  1. Using a free library, which would do all the complex work for you.
  2. Working with DataContracts that are built-in to the .NET framework.
  3. Writing your own “parser” for converting JSON into suitable format required.
In this article, I have used the free library named – JSON.NET. You can add this library to your project using the NuGet package manager. If you are not sure about adding the library, please refer to this article – Adding references using NuGet packages
 
Code Snippets
  1. public JObject ReadJSONData(string jsonFilename)    
  2.         {    
  3.             try    
  4.             {    
  5.                 JObject jObject;    
  6.                 // Read JSON directly from a file    
  7.                 using (StreamReader file = System.IO.File.OpenText(jsonFilename))    
  8.                 using (JsonTextReader reader = new JsonTextReader(file))    
  9.                 {    
  10.                     jObject = (JObject)JToken.ReadFrom(reader);    
  11.                 }    
  12.                 return jObject;    
  13.             }    
  14.             catch (Exception ex)    
  15.             {    
  16.                 Console.WriteLine("Error Occurred : " + ex.Message);    
  17.                 return null;    
  18.             }    
  19.         }    
  20.     
  21. //Read calendar info from JSON file    
  22.  JObject jObject = ReadJSONData(jsonFilename);    
  23.  for (int i = 0; i < jObject["employee"].Count(); i++)    
  24.  {    
  25.       //Your logic goes here...    
  26.  }   
Example of JSON file
  1. {  
  2.     "employee": [{  
  3.         "name""Shankar",  
  4.         "empid""1",  
  5.         "department""Design",  
  6.     }, {  
  7.         "name""Ashish",  
  8.         "empid""2",  
  9.         "department""Testing",  
  10.     }, {  
  11.         "name""Dipendra",  
  12.         "empid""3",  
  13.         "department""Development",  
  14.     }]  
I hope you enjoyed this article. Keep exploring and using JSON with ASP.NET. Let me know in case if you feel there is an issue with the article or if you didn’t follow any part of it.
 
What do you think?
 
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.
 
Read more articles on ASP.NET