C#: Useful JSON in .Net


C#: Useful JSON in .Net

Welcome to CodeSpread!


All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from database and sometimes from XMLs. As a result of which we end up hitting DB/File system several times and ultimately compromises with Page speed and performance.


JSON (JavaScript Object Notation) is the solution to avoid all the difficulties with respect to data.

Today we will try our hands on using JSON in .Net:

The very first thing which is required to use JSON in our code is a namespace.


Now the most important class, JavaScriptSerializer

Let the play begin:

Consider you have a DataTable dt filled with all your DB contents

(Columns: UID, Firstname, Lastname, Mobileno, Address and many more) and you need to have these data handy.

Then Query this data by linq query.


var details = from data in dt.AsEnumerable()

select new


UID = data.Field("UID"),

FirstName = data.Field("FirstName"),

LastName  = data.Field("LastName”),

Mobileno = data.Field("Mobileno"),

Address = data.Field<string>("Address")


This code has created an anonymous type (Read more about Anonymous type here )


Now this variable ‘details’ can be serialized to a JSON string as shown below.

string JsonString = string.Empty;

JavaScriptSerializer jsSerialize = new JavaScriptSerializer();

JsonString = jsSerialize.Serialize(details); //Created above

This ‘Jsonstring’ variable will be available to our .aspx (design view) for display or any other operation as per the request

The data in the string looks like this:



"UID": 1316134065,

"Firstname": Jason,

"Lastname": "Bourne",

"Mobileno": "1234567890",

"Address": "XYZ"



"UID": 1316134065,

"Firstname": Jason,

"Lastname": "Bourne",

"Mobileno": "1234567890",

"Address": "XYZ"



On the similar lines, you can do a vice-versa action too. Wondering how? Let’s see how to parse a JSON object using the same class JavaScriptSerializer.

Create an instance of JavaScriptSerializer and then call Deserialize function to work on the JSON string which return a Dictionary object Dictionary.


string json = “[{“Firstname”:”Test”, . . . ”;
JavaScriptSerializer jsSerialize = new JavaScriptSerializer ();

Dictionary dict = jsSerialize.Deserialize(json);

Now you have the Dictionary to play around with the data.

Enjoy coding!!!

Author: jackie

I am a developer.

Share This Post On


  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...
  • How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010? 1. Open VS2010. 2. Select File -> New Project. 3. Select Visual C# -> Web -> ASP.NET MVC 2 Empty Web Application. Name it as MyMvcApplicat...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • Simply LINQ: What,Why and How.Simply LINQ: What,Why and How.What is LINQ? LINQ is Language-Integrated Query (LINQ). It is primarily an extension to the language's capability where it adds query expressions which processes objects and data. It is a one s...
  • Don’t Mess with App_OfflineDon’t Mess with App_OfflineOne of the most useful features of ASP.NET is the "App_Offline.htm" . It provides a much easy way to make the ASP.NET application offline while we do our modifications on the site. How we do it? ...
  • C#: My experience with const keywordC#: My experience with const keywordThere are numerous post on internet about 'what is const keyword in .net' or 'what is the difference between const keyword and read only' and I must say many might have already mugged it up like me...
  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...