1

Below is the code from where I want to return JSON of EmployeeDetails table.

[HttpGet]
            [Route("AllEmployeeDetailsInJSON")]
            public IQueryable<EmployeeDetail> GetEmployeeInJSON()
            {
                try
                {
                    return objEntity.EmployeeDetails;
                }
                catch (Exception)
                {
                    throw;
                }
            }
Cleptus
  • 3,446
  • 4
  • 28
  • 34
Abhi
  • 614
  • 2
  • 12
  • 26
  • Basically it is not different to seriliaze any other object to json please have a look this question https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net – nzrytmn Oct 24 '19 at 07:12

2 Answers2

2
public ActionResult Index()
{
     dbcontext db = new dbcontext();
     return View(db.EmployeeDetails.ToList());
}

To get JSON

public System.Web.Mvc.JsonResult GetEmployeeInJSON()
{
     dbcontext db = new dbcontext();
     var data = db.EmployeeDetails.ToList();
     return Json(data, JsonRequestBehavior.AllowGet);
}

If you are trying to use it in webapi. use following code.

While calling api, set Content-Type = "application/json"

[HttpGet]
[Route("AllEmployeeDetailsInJSON")]
public async Task<HttpResponseMessage> GetEmployeeInJSON()
{   
    try
    {
        dbcontext db = new dbcontext();
        var data = db.EmployeeDetails.ToList();
        return Request.CreateResponse(HttpStatusCode.OK, new
        {
            Data = data
        });
    }
    catch (Exception)
    {
        throw;
    }
}
  • I don't see anything related to JSON in your answer. Please update and add some commentary information as well to support your answer. – Barrosy Oct 24 '19 at 06:57
  • You haven't added any explanation to your answer whatsoever. Maybe you could make this clear for OP so he or she might understand what kind of solution you are providing. For example, OP is asking for a web API solution, so why did you choose for an MVC related method? – Barrosy Oct 24 '19 at 07:13
1

So basically you have two options when you work with Web API:

First One: Use JSON.NET and return a string

You could use the Newtonsoft Nuget package. You can convert every object to JSON string with one line of code:

public string GetEmployees(){
        //Get Employees from Db:
        var employessFromDb = _context.Employees.ToList(); //Where Employees is your table name


        //Generate JSON string that we want to return
        string JSON = JsonConvert.SerializeObject(employessFromDb);

        //return the JSON
        return JSON;
}

The problem with returning a string is that your JSON will be in quotation marks and you will not send any HTTP code.

So I would use the second approach:

Second: Use the build in solution

public IHttpActionResult GetCustomers() 
    {
        var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
        return Ok(employeesFromDb);
    }

The second approach will convert the Employees list after you return it so you don't have to care about that

No matter what way you choose, you will need a (global) variable:

private ApplicationDbContext _context;

So if you combine the given code and my answer ist would be something like this:

    [HttpGet]
    [Route("AllEmployeeDetailsInJSON")]
    public IHttpActionResult GetEmployeeInJSON()
    {
        try
        {
            var employeesFromDb = _context.Employees.ToList(); //Where Employees is your table name
            return Ok(employeesFromDb);
        }
        catch (Exception)
        {
            throw;
        }
    }
MarvinS
  • 84
  • 1
  • 11
  • Hi Marvin, as I am new to this so could you please elaborate your answer as per code given. – Abhi Oct 24 '19 at 07:08
  • I've changed my answer with code that worked in my case. Please let me know if that is something you were looking for – MarvinS Oct 24 '19 at 07:52