Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
22 Dec
2022

Reading json data from file using Asp.Net

by Shubham Batra

981

Download Attachment


JSON File-

So for this tutorial we will use a file which will have json data in it and we will place that in the Content folder of the project. As you can see in the image below

Now for reading this json file in C# we have to use StreamReader class. You have to go to controller and copy this code


using JsonReader.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JsonReader.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {   //Reading file from server 
            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);
        }

    }
}

For parse the json here we are using NewtonSoft.Json Library which you can get from nuget packages. You have to also create following class to parse json data to list object

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;}
    }
}

Now run the application and you will be able to see the data being read from json file to list object which we can show on  screen like the screenshot below

So this is how we can read json data from json file and parse in list object in asp.net.

  • |
  • AspNet , StreamReader , Json to C#

Comments

Follow Us On Social Media - Like Us On Facebook

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

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
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

Continue with Downloading

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 © 2023 by Code2night. All Rights Reserved

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