Web服务返回JSON数组而不是JSON对象

时间:2016-06-09 09:19:44

标签: .net json asp.net-mvc

我试图使用.NET MVC5编写的Web服务返回我可以在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"
    }]
}]

3 个答案:

答案 0 :(得分:2)

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

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

...然后在C#中对您的模型执行相同的操作:创建一个PeopleModel类,其People属性类型为IEnumerable<PersonModel>

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

然后,您可以实例化PeopleModel个实例,将PeopleModel.People设置为IEnumerable<PersonModel>return PeopleModel

答案 1 :(得分:0)

使用<div class="form-group blu-margin"> <select class="form-control" th:field="${operator.opeId}" id="dropOperator"> <option value="0">select operator</option> <option th:each="operator : ${operators}" th:value="${operator.id}" th:text="${operator.operatorName}"></option> </select> </div>

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

请参阅此link了解详情

答案 2 :(得分:-1)

<强>更新

以下答案与Dawood Alan在下面的评论中指出的无关。

  

而不是返回人员尝试返回以下内容。

return JsonConvert.SerializeObject(person);
     

您还需要在控制器上调整返回类型。

     

您可能需要通过程序包管理器为此添加所需的程序包   这样做的时候。

Install-Package Newtonsoft.Json