Parsing A JSON File With C#

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:

  1. 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;  
            }  
        }  
    }
  2. 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.


Similar Articles