-1

There is a JSON file on My Asp.net core APi project.

I want this API to grab that file and then read all the records within that file so it is ready to display and available for requests.

For example I have list of employees in my employee.json file:

   {
  "EmployeeList": [
    {
      "EmpId": 1,
      "EmpName": "Krish",
      "EmpCode": 1001,
      "JoiningDate": "12/07/2021",
      "DeptId": 1,
      "DesgId": 2
    },
    {
      "EmpId": 2,
      "EmpName": "Prish",
      "EmpCode": 1002,
      "JoiningDate": "17/02/2021",
      "DeptId": 3,
      "DesgId": 1
    },
    {
      "EmpId": 3,
      "EmpName": "Krishna",
      "EmpCode": 1003,
      "JoiningDate": "12/04/2020",
      "DeptId": 2,
      "DesgId": 2
    },
    {
      "EmpId": 4,
      "EmpName": "Ram",
      "EmpCode": 1004,
      "JoiningDate": "12/07/2021",
      "DeptId": 1,
      "DesgId": 2
    },
    {
      "EmpId": 5,
      "EmpName": "Rahul",
      "EmpCode": 1005,
      "JoiningDate": "10/07/2021",
      "DeptId": 3,
      "DesgId": 2
    },
    {
      "EmpId": 6,
      "EmpName": "Raja",
      "EmpCode": 1006,
      "JoiningDate": "12/01/2021",
      "DeptId": 1,
      "DesgId": 2
    },
    {
      "EmpId": 7,
      "EmpName": "Hiya",
      "EmpCode": 1007,
      "JoiningDate": "2/07/2021",
      "DeptId": 3,
      "DesgId": 2
    },
    {
      "EmpId": 8,
      "EmpName": "Riya",
      "EmpCode": 1008,
      "JoiningDate": "2/05/2021",
      "DeptId": 1,
      "DesgId": 3
    },
    {
      "EmpId": 9,
      "EmpName": "Sheela",
      "EmpCode": 1009,
      "JoiningDate": "12/10/2021",
      "DeptId": 1,
      "DesgId": 2
    },
    {
      "EmpId": 10,
      "EmpName": "Komal",
      "EmpCode": 1010,
      "JoiningDate": "12/01/2021",
      "DeptId": 1,
      "DesgId": 1
    }
  ]
 
}

Model Class Like this:

public class Employee
    {
        public List<EmployeeDetail> EmployeeList { get; set; }
    }
    public class EmployeeDetail
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; }
        public int EmpCode { get; set; }
        public DateTime JoiningDate { get; set; }
        public int DeptId { get; set; }
        public int DesgId { get; set; }

    }

Note : i dont want use database using json file and module class i need to get multiple records in Controller. Now My multiple records which i need to execute is

  1. Find all Employees's DepartmentId where DesgId=2

  2. Find distinct designation (i.e[{1}.{2}])

help me with some steps or code.

Mina
  • 1
  • 2

1 Answers1

1

According to your description, if the file is storing inside your web application, I suggest you could try to use IWebHostEnvironment to get the path and then use System.IO.File.ReadAllText to read the json content.

Then you could use JsonConvert.DeserializeObject to deserialize the json's content to a list.

After converting to the list, you could use LINQ to put some selector to that List.

More details, you could refer to below example codes:

Model class:

    public class Employee 
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; }
        public int EmpCode { get; set; }
        public string JoiningDate { get; set; }
        public int DeptId { get; set; }
        public int DesgId { get; set; }
    }

    public class Root
    {
        public List<Employee> EmployeeList { get; set; }
    }

Controller's codes:

    private readonly IWebHostEnvironment _hostingEnvironment;

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger , IWebHostEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;

        _logger = logger;
    }

    [HttpGet]
    public IActionResult Get(int id)
    {
        var rootPath = _hostingEnvironment.ContentRootPath; //get the root path

        var fullPath = Path.Combine(rootPath, "employee.json"); //combine the root path with that of our json file inside mydata directory

        var jsonData = System.IO.File.ReadAllText(fullPath); //read all the content inside the file


        Root root = JsonConvert.DeserializeObject<Root>(jsonData);

        var  result =  root.EmployeeList.Where(x => x.DeptId == 2).ToList() ;
       return result ;}

Result: enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65