0

I have following simple code which is giving list of employee , i have not added any formatter getting result in json array format.

I want it as a json object which has one defined key , how can i achieve this?

 public HttpResponseMessage GetEmployee([FromUri(Name ="")]FilterEntity filters)
 {
      try
      {
           string contentType=Request.Headers.Accept.ToString();        
           if (String.Equals(contentType, Ecng_APIMsg.ContentType))
           {
                var empDetails = _empService.GetEMployee(filters.systemId, filters.pageIndex, filters.pageSize, filters.sortDirection);
                if (empDetails != null)
                { 
                     return Request.CreateResponse(HttpStatusCode.OK, empDetails);
                }
           }
      }
      catch(Exception ex)
      {
      }
 }

output-

[
  {
    "Id": 43,
    "EmpId": 11
  },
  {
    "Id": 42,
    "EmpId": 12
  }
]

expected

"data" : [
  {
    "Id": 43,
    "EmpId": 11
  },
  {
    "Id": 42,
    "EmpId": 12
  }
]

data must be there as a key of that json object. I tried like below code to achieve this which is not working I'm missing something here -

var response = Request.CreateResponse(HttpStatusCode.OK, empDetails);  
   response.Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json");
   return response;
2
  • 1
    What is this 'var empDetails' ? is it a list of Employee class with properties "Id" and "EmpId" ? Commented May 30, 2017 at 6:12
  • empDetails is IEnumerable Commented May 30, 2017 at 6:27

3 Answers 3

1

Hope in your code "var empDetails" is a List<Employee> (List of Employee class)

As per your JSON structure i guess your Employee class would be like below

public class Employee
{
    public int Id{ get; set; }
    public int EmpId{ get; set; }
}

That's the reason when you are returning the json structure, it returns as

[
  {
    "Id": 43,
    "EmpId": 11
  },
  {
    "Id": 42,
    "EmpId": 12
  }
]

if you want your json data need to be changed as you expect, then create another class named as EmployeeVM

public class EmployeeVM
{
    public List<Employee> data{ get; set; }        
}

and construct your EmployeeVM from your code and return EmployeeVM as json from your web api.

Sign up to request clarification or add additional context in comments.

Comments

1

create another class as below,

public class clsDemo
{
    public Object data { get; set; }
}

use,

clsDemo ObjDemo = new clsDemo();
ObjDemo.data = empList;//Your 'empDetails'
String result = JsonConvert.SerializeObject(ObjDemo);

Comments

0

You could create an object to wrap the collection. Using an anonymous object can be done as a quick and simple solution.

return Request.CreateResponse(HttpStatusCode.OK, new { data = empDetails });

Based on original example in question the above update to the code would yield

{
  "data" : [
    {
      "Id": 43,
      "EmpId": 11
    },
    {
      "Id": 42,
      "EmpId": 12
    }
  ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.