How to Read list of objects from json file and convert it into list of objects in c# .net core

  public List<CityMaster> getcities()

        {

            List<CityMaster> cities = new List<CityMaster>();

            try

            {

                var res = new List<CityMaster>();

                string path1 = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\assets\citieslist.json"}";

                using (StreamReader r = new StreamReader(path1))

                {

                    string json = r.ReadToEnd();

                    res = JsonConvert.DeserializeObject<List<CityMaster>>(json);

                }               

                if (res.Count() > 0 && res != null)

                {

                    cities = res.ToList();

                }

            }

            catch (Exception ex)

            {


            }

            return cities;

        }

Comments

Popular posts from this blog

Implement virtualisation in kendo drop down list using jquery ajax in mvc, aspx page

How to Create trigger to Update the NoofOrgs Count under each category in Category Table based on Temp Table in Sql Server

How to Preview the image and download which is in base64string format in mvc view using C#