Create A JSON File With C#

In previous article, I discussed about JSON. To see my previous article here is the link: What is JSON

We can create a JSON file by using:
JavaScriptSerializer class : built in dot net framework

 If you already know MVC with Entity Framework Code first approach you can skip Step 1 to Step 4.

I am using visual studio 2013 for this demo.

Step 1:  Open visual studio ( for this open run command by pressing windows flag key + R now run command open type devenv, then click ok ).

Go to File, New, then Project and select C#. After that select Web, then ASP.NET Web Application. Name the application CreatingJsonFile and click OK, Select MVC template and Change Authentication to No Authentication and click ok.


Step 2:  Now from solution explorer, right click on model folder, add a class, name the class Person.cs and add some properties.


Step 3:  Now right click on controller folder, add a Controller and select MVC5 controller with view using Entity Framework,  then click add.



Now select model class Person and add New Data Context class. Here I am not changing the Context and Controller name (if you want to change the Context and Controller name you can change). Controller will also generate views for you.



Step 4:  
Go to App_start folderMAND  click on RoutConfig.cs file change the Controller name from Home to People so that when you run the application, the starting page should be People/index.



Step 5:
 Now click on F5 to run the application and when you run the application first time, it will create database with table name People in SQL Express.



Now add some data into table, click on Create New link and it will open form, fill the form and click on create, so in this way I have added 4 records.



The following records are in my database table People.



Step 6:
In this step I am going to create a JSON file using JavaScriptSerializer Class. JavaScriptSerializer class lies in System.Web.Script.Serialization namespace and this namespace found in System.web.Extensions.dll.

So to use JavaScriptSerializer class you must add the reference of System.web.Extensions.dll into your project.

 

Now modify the Person Controller.

  1. // GET: People  
  2. [HttpGet]  
  3. public ActionResult Index()  
  4.     {  
  5.         return View(db.People.ToList());  
  6.     }  
  7.     // Create Json file  
  8.     //here i am not passing anything from view to controller  
  9.     [HttpPost]  
  10. public ActionResult Index(Person obj)  
  11. {  
  12.     // retrive the data from table  
  13.     var personlist = db.People.ToList();  
  14.     // Pass the "personlist" object for conversion object to JSON string  
  15.     string jsondata = new JavaScriptSerializer().Serialize(personlist);  
  16.     string path = Server.MapPath("~/App_Data/");  
  17.     // Write that JSON to txt file,  
  18.     System.IO.File.WriteAllText(path + "output.json", jsondata);  
  19.     TempData["msg"] = "Json file Generated! check this in your App_Data folder";  
  20.     return RedirectToAction("Index");;  
  21. }   

In Index page I have added a submit button, when user will click on this bUtton it will hit the Index post action method.

Now press F5 and you will the following screen:



Now click on button and you will get the following screen.



Here is my generated json file.



So indirectly we learned Serialization.

What is Serialization

According to MSDN, it is a process of converting an object into a stream of byte in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to recreate it when needed.

You can download the source code from my github account.

Point of Interest

In this article we learned MVC with Entity Framework Code First Approach and how to create a JSON file using JavascriptSerializer class and what is Serialization.