HOW TO PARSING A JSON FILE WITH C# MVC.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 847  View(s)
Ratings:
Rate this:
I want to use Parsing A JSON File With C# Mvc. How to do this please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to Parsing A JSON File With C# mvc.

     

     @model IEnumerable<ForumMVC.Models.Customer>  
        <!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.EmailId) 
     
                        </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.EmailId) 
     
                            </td> 
                        </tr> 
                    } 
     
                </table> 
            </body> 
        </html> 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Web;

    namespace ForumMVC.Models
    {
        public class Customer
        {
            public int CustomerId
            {
                get;
                set;
            }
            public string FirstName
            {
                get;
                set;
            }
            public string LastName
            {
                get;
                set;
            }
            public string EmailId
            {
                get;
                set;
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using ForumMVC;
    using System.Data.Entity;
    using ForumMVC.Models;
    using System.Web.Script.Serialization;
    namespace ForumMVC.Controllers
    {
        public class HomeController : Controller
        {
           
            public ActionResult Index()
            {           
                string file = Server.MapPath("~/App_Data/Customer.json");       
                string Json = System.IO.File.ReadAllText(file);
                JavaScriptSerializer ser = new JavaScriptSerializer();
                var  list = ser.Deserialize<List<Customer>>(Json);
                return View(list);            
            }

        }
    }



        "CustomerId": 1, 
        "FirstName": "sudhesh", 
        "LastName": "Patel", 
        "EmailId": "sudhesh@gmail.com
    }, 

        "CustomerId": 1, 
        "FirstName": "suresh", 
        "LastName": "Patel", 
        "EmailId": "suresh@gmail.com
    }, 

        "CustomerId": 1, 
        "FirstName": "Shiv Shingh", 
        "LastName": "Patel", 
        "EmailId": "shiv@gmail.com
    }, 

        "CustomerId": 1, 
        "FirstName": "manoj", 
        "LastName": "Patel", 
        "EmailId": "manoj@gmail.com
    }, 

        "CustomerId": 1, 
        "FirstName": "mayank", 
        "LastName": "Patel", 
        "EmailId": "manoj@gmail.com
    }, 

        "CustomerId": 1, 
        "FirstName": "Mohit", 
        "LastName": "Patel", 
        "EmailId": "mohit@yahoo.com
    }]

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!