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
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
[ { "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: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:@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, 7:25 PM
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, 6:24 AM
It was a great article
prajwal cPosted Mar 5, 2018, 8:17 AM
Ah!thanks for this info.....helped a lot
Sasi RekhaPosted Aug 7, 2017, 4:11 AM
It was great article,thanks
Ravi PatelPosted Oct 28, 2015, 1:32 PM
Thanks Vivek
Vivek TripathiPosted Oct 28, 2015, 1:09 PM
nice one
Ravi PatelPosted Oct 28, 2015, 10:58 AM
Thanks Gowtham
Gowtham KPosted Oct 28, 2015, 10:56 AM
Good One
Ravi PatelPosted Oct 28, 2015, 9:54 AM
thanks Mohammed
Mohammed IbrahimPosted Oct 28, 2015, 9:25 AM
nice
Ravi PatelPosted Oct 28, 2015, 8:49 AM
thanks Sir!
Nilesh JadavPosted Oct 28, 2015, 8:30 AM
Good work
Ravi PatelPosted Oct 28, 2015, 7:54 AM
thanks Sibeesh!
Sibeesh VenuPosted Oct 28, 2015, 7:27 AM
Nice Share
Anu VPosted Oct 28, 2015, 7:08 AM
Thanks
Ravi PatelPosted Oct 28, 2015, 6:10 AM
thanks
Mukesh KumarPosted Oct 28, 2015, 5:12 AM
Good Job
Ravi PatelPosted Oct 28, 2015, 5:01 AM
thanks!
Humayun Kabir MamunPosted Oct 28, 2015, 5:00 AM
Nice...