【问题标题】:Web Service Returning JSON Array not JSON ObjectWeb 服务返回 JSON 数组而不是 JSON 对象
【发布时间】:2016-06-09 09:19:43
【问题描述】:

我正在尝试让我的 Web 服务(用 .NET MVC5 编写)返回我可以在 iOS 应用程序中使用的 JSON。我以为我已经成功了,但是更仔细地查看返回的数据 - 请记住,这对我来说是全新的,所以请原谅我的术语 - 看起来我得到的是 JSON 数组而不是 JSON 对象.

我认为,当我按照在线教程展示如何将 JSON 对象转换为 Swift 中的字典以在应用程序中显示时,这会导致问题。正如您从下面的输出示例中看到的那样,我的 JSON 以 [{"FirstName":"John"... 开头,有效地直接启动到 People 数组中,而我想我希望它以 {"People":[{"FirstName":"John"... 之类的开头

如何将 JSON 返回为一个对象而不仅仅是一组人?我希望我快到了,也许只需要在某个地方更改类型?

型号:

public class PersonModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DepartmentName { get; set; }

    public IEnumerable<PhoneNumberModel> PhoneNumbers { get; set; }
}

控制器:

public class PersonController : ApiController
{
    public IEnumerable<PersonModel> GetAllPersons()
    {
        List<PersonModel> person;
        using (var context = new ContactsContext())
        {
            person = context.People.Include("PhoneNumbers.PhoneNumberType1").ToList()
                .Select(p => new PersonModel
                {
                    FirstName = p.FirstName,
                    LastName = p.LastName,
                    DepartmentName = p.Department1.Name,
                    PhoneNumbers = p.PhoneNumbers.Select(x => new PhoneNumberModel
                    {
                        PhoneNumberTypeName = x.PhoneNumberType1.Description,
                        TelephoneNumber = x.PhoneNumber1
                    })
                }).ToList();
        }

        return person;
    }
}

输出:

[{
"FirstName": "John",
"LastName": "Smith",
"DepartmentName": "Accounts",
"PhoneNumbers": [{
    "PhoneNumberTypeName": "Office",
    "TelephoneNumber": "12345"
    }, {
    "PhoneNumberTypeName": "Mobile",
    "TelephoneNumber": "54321"
    }]
}, {
    "FirstName": "Jane",
    "LastName": "Harris",
    "DepartmentName": "HR",
    "PhoneNumbers": [{
        "PhoneNumberTypeName": "Mobile",
        "TelephoneNumber": "98765"
    }]
}]

【问题讨论】:

  • 生成的 JSON 是正确的 - 您的 Action 的返回类型是 IEnumerable&lt;PersonModel&gt;,它是一个集合,因此它始终会转换为 JSON 中的数组。

标签: .net json asp.net-mvc


【解决方案1】:

您的GetAllPersons 方法返回一个IEnumerable(即PersonModel 的集合),因此它将被序列化为其对应的JSON(即JSON 对象的集合)。如果要将集合包装在 JSON 对象中,则变为:

{
   "People": [
      {
         "FirstName": "trashr0x",
         "LastName": "StackOverflow",
         "DepartmentName": "MVC",
         "PhoneNumbers": [
            {
               "PhoneNumberTypeName": "Work",
               "TelephoneNumber": "123456"
            }
         ]
      }
   ]
}

...然后在 C# 中对您的模型执行相同的操作:创建一个具有 People 类型为 IEnumerable&lt;PersonModel&gt; 的属性的 PeopleModel 类:

public class PeopleModel
{
    public IEnumerable<PersonModel> People { get; set; }
}

然后您可以实例化一个PeopleModel 实例,将PeopleModel.People 设置为您的IEnumerable&lt;PersonModel&gt;return PeopleModel

【讨论】:

  • 这成功了,谢谢!是否可以预期这样做会更慢地返回数据?因为它是!
  • 嘿,不客气。它通常不应该 - 如果不查看您的实现,我无法判断。
【解决方案2】:

使用HttpResponseMessage

public HttpResponseMessage GetAllPersons()
{
    List<PersonModel> person;
    using (var context = new ContactsContext())
    {
        person = context.People.Include("PhoneNumbers.PhoneNumberType1").ToList()
            .Select(p => new PersonModel
            {
                FirstName = p.FirstName,
                LastName = p.LastName,
                DepartmentName = p.Department1.Name,
                PhoneNumbers = p.PhoneNumbers.Select(x => new PhoneNumberModel
                {
                    PhoneNumberTypeName = x.PhoneNumberType1.Description,
                    TelephoneNumber = x.PhoneNumber1
                })
            }).ToList();
    } 
    HttpResponseMessage response = 
    Request.CreateResponse(HttpStatusCode.OK, new { People = person });
    return response;  
  }

使用IHttpActionResult

public IHttpActionResult GetAllPersons()
    {
        List<PersonModel> person;
        using (var context = new ContactsContext())
        {
            person = context.People.Include("PhoneNumbers.PhoneNumberType1").ToList()
                .Select(p => new PersonModel
                {
                    FirstName = p.FirstName,
                    LastName = p.LastName,
                    DepartmentName = p.Department1.Name,
                    PhoneNumbers = p.PhoneNumbers.Select(x => new PhoneNumberModel
                    {
                        PhoneNumberTypeName = x.PhoneNumberType1.Description,
                        TelephoneNumber = x.PhoneNumber1
                    })
                }).ToList();
        } 
        return Ok(new { People = person }); 
    }

查看此link 了解更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2023-04-05
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    相关资源
    最近更新 更多