We can parse a JSON file using JavaScriptSerializer class.
Before reading this article, I would recommend just go through once to my previous article because I am going to use model class and JSON file of the previous article:
Previously I had done the following:
-
Person model class created
C#using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace CreatingJsonFile.Models { public class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string City { get; set; } } } -
JSON File created
JavaScript[ { "PersonId": 1, "FirstName": "Ravi", "LastName": "Patel", "City": "Varanasi" }, { "PersonId": 2, "FirstName": "Ranjeet", "LastName": "Patel", "City": "Delhi" }, { "PersonId": 3, "FirstName": "Rajendra", "LastName": "Patel", "City": "Varanasi" }, { "PersonId": 4, "FirstName": "Aarav", "LastName": "Patel", "City": "Bangalore" }]
Now in this article we will use JavaScriptSerializer class to deserialize JSON from file.
-
To use JavaScriptSerializer class you must add the reference of System.Web.Extensions.dll into your project.
-
Now right click on Controller folder, go to add, then controller, click on it and select MVC5 Controller -Empty and name ReadJsonController.

Now the JSON Controller looks like the following:

Now modify Index Action method:C#using System.Web; using System.Web.Mvc; using CreatingJsonFile.Models; using System.Web.Script.Serialization; // for serialize and deserialize using System.IO; // for File operation namespace CreatingJsonFile.Controllers { public class ReadJsonController: Controller { // GET: ReadJson public ActionResult Index() { //get the Json filepath string file = Server.MapPath("~/App_Data/output.json"); //deserialize JSON from file string Json = System.IO.File.ReadAllText(file); JavaScriptSerializer ser = new JavaScriptSerializer(); var personlist = ser.Deserialize < List < Person >> (Json); return View(personlist); } } } -
Now right click on Index() method and click on add view, keep view name as index and select List template. Select Person model class and click on add.

So view is generated and here's the code snippet:JavaScript@model IEnumerable <CreatingJsonFile.Models.Person> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <p></p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.FirstName) </th> <th> @Html.DisplayNameFor(model => model.LastName) </th> <th> @Html.DisplayNameFor(model => model.City) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.FirstName) </td> <td> @Html.DisplayFor(modelItem => item.LastName) </td> <td> @Html.DisplayFor(modelItem => item.City) </td> </tr> } </table> </body> </html> -
Now run the application. You will get the following screen,

Point of Interest
In this article we learned how to parse JSON file with C# and desrialization.


Keorapetsi MatsemePosted Feb 13, 2022, 2:25 PM EST
Great article . Now how can the same be archived with json array, example:{ "People": [ [ "Doe", "John", "1982/10/08" ], [ "Wayne", "Bruce", "1965/01/30" ], [ "Gaga", "Lady", "1986/03/28" ], [ "Mark", "Curry", "1988/02/29" ] ] }
Suresh ThakurPosted Jan 15, 2019, 1:24 AM EST
It was a great article
prajwal cPosted Mar 5, 2018, 3:17 AM EST
Ah!thanks for this info.....helped a lot
Sasi RekhaPosted Aug 7, 2017, 12:11 AM EDT
It was great article,thanks
Ravi PatelPosted Oct 28, 2015, 9:32 AM EDT
Thanks Vivek
Vivek TripathiPosted Oct 28, 2015, 9:09 AM EDT
nice one
Ravi PatelPosted Oct 28, 2015, 6:58 AM EDT
Thanks Gowtham
Gowtham KTOP 100Posted Oct 28, 2015, 6:56 AM EDT
Good One
Ravi PatelPosted Oct 28, 2015, 5:54 AM EDT
thanks Mohammed
Mohammed IbrahimPosted Oct 28, 2015, 5:25 AM EDT
nice
Ravi PatelPosted Oct 28, 2015, 4:49 AM EDT
thanks Sir!
Nilesh JadavPosted Oct 28, 2015, 4:30 AM EDT
Good work
Ravi PatelPosted Oct 28, 2015, 3:54 AM EDT
thanks Sibeesh!
Sibeesh VenuTOP 100Posted Oct 28, 2015, 3:27 AM EDT
Nice Share
Anu VPosted Oct 28, 2015, 3:08 AM EDT
Thanks
Ravi PatelPosted Oct 28, 2015, 2:10 AM EDT
thanks
Mukesh KumarPosted Oct 28, 2015, 1:12 AM EDT
Good Job
Ravi PatelPosted Oct 28, 2015, 1:01 AM EDT
thanks!
Humayun Kabir MamunPosted Oct 28, 2015, 1:00 AM EDT
Nice...