How to read json file in asp.net mvc | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

How to read json file in asp.net mvc

Date- Feb 05,2024

2974

Free Download Pay & Download
Reading json file

Reading JSON File in ASP.NET MVC

In this article we will see how we can read json file and convert that into c# object in asp.net mvc.

In this article, we'll explore a simple HomeController class in an ASP.NET MVC application that reads data from a JSON file.

The Code

Let's take a closer look at the CountryCodes class    which we will use to deserialize the json data.

        
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            
            namespace JsonReader.Models
            {
                public class CountryCode
                {
                    public string countryname { get; set; }
                    public string continent { get; set; }
                    public string currency { get; set; }
                    public string capital { get; set; }
                    public string timezoneincapital { get; set; }
                }
            }
        
    

This is the controller code where we will read the sample json file and try to convert that to c# list object

        
            public class HomeController : Controller
            {
                public ActionResult Index()
                {
                    List<CountryCode> items = new List<CountryCode>();
                    using (StreamReader r = new StreamReader(Server.MapPath("/Content/countrycodes.json")))
                    {
                        string json = r.ReadToEnd();
                        items = JsonConvert.DeserializeObject<List<CountryCode>>(json);
                    }
                    return View(items);
                }
            }
        
    

Explanation

This code resides in a controller class, HomeController. It defines an Index action that reads JSON data from a file named countrycodes.json.

Let's break down the key components of the code:

  • StreamReader: Initializes a new instance of the StreamReader class to read from the specified file path.
  • Server.MapPath: Maps the virtual path to a physical path on the server. In this case, it resolves the path to the countrycodes.json file.
  • JsonConvert.DeserializeObject: Deserializes the JSON data read from the file into a List<CountryCode> object.
  • return View(items): Passes the deserialized data to the corresponding view for further processing or rendering.

This code assumes the existence of a class named CountryCode representing the structure of the JSON data. Make sure to replace it with your actual class definition.

Conclusion

Reading JSON files in ASP.NET MVC is a common task, and the provided code demonstrates a straightforward approach to achieve this. Feel free to adapt and customize it based on your specific requirements.

So this is how to read json file in asp.net mvc.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1190
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts