Parsing A JSON File With C#

In this article you will learn how to parse a JSON file with C#.

We can parse a JSON file using JavaScriptSerializer class.

Before reading this article, I would recommended 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 did the following:

  1. Person model class created

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. namespace CreatingJsonFile.Models  
    6. {  
    7.     public class Person  
    8.     {  
    9.         public int PersonId  
    10.         {  
    11.             get;  
    12.             set;  
    13.         }  
    14.         public string FirstName  
    15.         {  
    16.             get;  
    17.             set;  
    18.         }  
    19.         public string LastName  
    20.         {  
    21.             get;  
    22.             set;  
    23.         }  
    24.         public string City  
    25.         {  
    26.             get;  
    27.             set;  
    28.         }  
    29.     }  

  2. JSON File created
    1. [  
    2. {  
    3.     "PersonId": 1,  
    4.     "FirstName""Ravi",  
    5.     "LastName""Patel",  
    6.     "City""Varanasi"  
    7. },  
    8. {  
    9.     "PersonId": 2,  
    10.     "FirstName""Ranjeet",  
    11.     "LastName""Patel",  
    12.     "City""Delhi"  
    13. },  
    14. {  
    15.     "PersonId": 3,  
    16.     "FirstName""Rajendra",  
    17.     "LastName""Patel",  
    18.     "City""Varanasi"  
    19. },  
    20. {  
    21.     "PersonId": 4,  
    22.     "FirstName""Aarav",  
    23.     "LastName""Patel",  
    24.     "City""Bangalore"  
    25. }]
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:

    1. using System.Web;  
    2. using System.Web.Mvc;  
    3. using CreatingJsonFile.Models;  
    4. using System.Web.Script.Serialization; // for serialize and deserialize  
    5. using System.IO; // for File operation  
    6. namespace CreatingJsonFile.Controllers  
    7. {  
    8.     public class ReadJsonController: Controller  
    9.     {  
    10.         // GET: ReadJson  
    11.         public ActionResult Index()  
    12.         {  
    13.             //get the Json filepath  
    14.             string file = Server.MapPath("~/App_Data/output.json");  
    15.             //deserialize JSON from file  
    16.             string Json = System.IO.File.ReadAllText(file);  
    17.             JavaScriptSerializer ser = new JavaScriptSerializer();  
    18.             var personlist = ser.Deserialize < List < Person >> (Json);  
    19.             return View(personlist);  
    20.         }  
    21.     }  

  • 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:

    1. @model IEnumerable  
    2. <CreatingJsonFile.Models.Person>  
    3.     <!DOCTYPE html>  
    4.     <html>  
    5.         <head>  
    6.             <meta name="viewport" content="width=device-width" />  
    7.             <title>Index</title>  
    8.         </head>  
    9.         <body>  
    10.             <p></p>  
    11.             <table class="table">  
    12.                 <tr>  
    13.                     <th>  
    14.   
    15.                         @Html.DisplayNameFor(model => model.FirstName)  
    16.   
    17.                     </th>  
    18.                     <th>  
    19.   
    20.                         @Html.DisplayNameFor(model => model.LastName)  
    21.   
    22.                     </th>  
    23.                     <th>  
    24.   
    25.                         @Html.DisplayNameFor(model => model.City)  
    26.   
    27.                     </th>  
    28.                     <th></th>  
    29.                 </tr>  
    30.   
    31.                     @foreach (var item in Model) {  
    32.   
    33.   
    34.                     <tr>  
    35.                         <td>  
    36.   
    37.                             @Html.DisplayFor(modelItem => item.FirstName)  
    38.   
    39.                         </td>  
    40.                         <td>  
    41.   
    42.                             @Html.DisplayFor(modelItem => item.LastName)  
    43.   
    44.                         </td>  
    45.                         <td>  
    46.   
    47.                             @Html.DisplayFor(modelItem => item.City)  
    48.   
    49.                         </td>  
    50.                     </tr>  
    51.                 }  
    52.   
    53.             </table>  
    54.         </body>  
    55.     </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.