简体   繁体   English

如何在WebAPI C#中以JSON对象格式而不是JSON数组检索结果

[英]How to retrieve result in json object format instead json array in webapi c#

I have following simple code which is giving list of employee , i have not added any formatter getting result in json array format. 我有以下简单的代码给出了雇员的名单,我还not added any formatter result in json array格式获取result in json array

I want it as a json object which has one defined key , how can i achieve this? 我希望它作为具有一个已定义键的json对象,如何实现呢?

 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. data必须在那里作为该json对象的键。 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;

Hope in your code " var empDetails " is a List<Employee> (List of Employee class) 希望您的代码“ var empDetails ”中是List<Employee> (Employee类的列表)

As per your JSON structure i guess your Employee class would be like below 根据您的JSON结构,我猜您的Employee类如下

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 这就是返回json结构时返回的原因

[
  {
    "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 如果您希望按需要更改json数据,则创建另一个名为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. 并根据您的代码构造EmployeeVM,然后从您的Web API返回EmployeeVM作为json。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM