在C#代码中反序列化Json对象

时间:2014-03-31 19:22:13

标签: c# json serialization asp.net-web-api deserialization

我正在开发一个Angular / Web API应用程序。这是一个让我在家忙碌的项目,这样我就可以熟悉新技术和集成服务的方法。

一切顺利,直到我不得不开始更新数据。我正在使用WebApi 2.1

问题是从角度我需要更新Employee对象但是当我传递一个ID和Employee Json对象时,它在到达C#代码时没有正确反序列化。

这是我将数据从客户端传递到服务器时使用的代码:

employeeService.update({ id: $routeParams.employeeId }, { 'employee': result.object });

结果。对象如下:

[prototype]: {...}
    $id: "1"
    $promise: {...}
    $resolved: true
    ContactNo: "0834541784"
    Courses: [[object Object],[object Object]]
    Division: {...}
    DivisionId: 3
    EmployeeCode: "E9955"
    EmployeeId: 4
    EmployeeType: null
    EmployeeTypeId: 1
    FirstName: "Gytis"
    Gender: 2
    IdNumber: "8311016262773"
    Nationality: {...}
    NationalityId: 1
    Position: null
    PositionId: 1
    Surname: "Barzdukas"

[prototype]: {...} $id: "1" $promise: {...} $resolved: true ContactNo: "0834541784" Courses: [[object Object],[object Object]] Division: {...} DivisionId: 3 EmployeeCode: "E9955" EmployeeId: 4 EmployeeType: null EmployeeTypeId: 1 FirstName: "Gytis" Gender: 2 IdNumber: "8311016262773" Nationality: {...} NationalityId: 1 Position: null PositionId: 1 Surname: "Barzdukas"

它必须在C#中映射到的Employee类如下所示:

public class Employee 
    {
        [Key]
        public int EmployeeId { get; set;}
        [Required]
        public string EmployeeCode { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string IdNumber { get; set; }
        public string ContactNo { get; set; }
        public Gender Gender { get; set; }
        public int? PositionId { get; set; }
        public int NationalityId { get; set; }
        public int? EmployeeTypeId { get; set; }
        public int DivisionId { get; set; }
        public  Division Division { get; set; }
        public  ICollection<EmployeeCourse> Courses { get; set; }
        public  Position Position { get; set; }
        public  Nationality Nationality { get; set; }
        public  EmployeeType EmployeeType { get; set; }
     }

现在,当代码到达C#Web APi更新功能时,我的ID将填充正确的EmployeeID,但Employee参数为空(空对象)。 empty Parameter.. 当我将这种类型从员工更改为对象时,代码看起来像这样,我不能让这个对象解析成类型Employee? enter image description here

public class Employee { [Key] public int EmployeeId { get; set;} [Required] public string EmployeeCode { get; set; } public string FirstName { get; set; } public string Surname { get; set; } public string IdNumber { get; set; } public string ContactNo { get; set; } public Gender Gender { get; set; } public int? PositionId { get; set; } public int NationalityId { get; set; } public int? EmployeeTypeId { get; set; } public int DivisionId { get; set; } public Division Division { get; set; } public ICollection<EmployeeCourse> Courses { get; set; } public Position Position { get; set; } public Nationality Nationality { get; set; } public EmployeeType EmployeeType { get; set; } }

我希望有人可以请求帮助并告诉我如何将此对象纳入员工类

感谢。

2 个答案:

答案 0 :(得分:0)

您无法发布&#39;对象&#39;在MVC中。您需要发布员工类型:

public HttpResponseMessage PutEmployee(int id, Employee employee)
{
    ...

您可能遇到命名问题,需要将第二个参数重命名为与该类型不同的名称:

public HttpResponseMessage PutEmployee(int id, Employee viewModel)
{
    ...

答案 1 :(得分:0)

我明白了。问题是我通过我的对象的方式以及我设置WebApi路由的方式。经过一些阅读后,我改变了这些东西,现在一切正常。

感谢所有的帮助和建议。

我改变了WebApi Route From:

[Route("Employee/{id}/{employee}")]
[HttpPut]
public HttpResponseMessage PutEmployee(int id, EmployeeDto employee)
{...}    

[Route("Employee/{id}")]
[HttpPut]
public HttpResponseMessage PutEmployee(int id, EmployeeDto employee)
{...}  

然后在我的UI上我改变了代码如下:

角度控制器来自:

employeeService.update({ id: $routeParams.employeeId }, {employee: result.object});

employeeService.update({ id: $routeParams.employeeId }, result.object);

我现在改变了我的EmployeeService(更新):

update: {
       method: "PUT", params: {
       EmployeeCode: "Default",
       FirstName: "Jan",
       Surname: "Solvent",
       IdNumber: "8508174141444",
       ContactNo: "0834411252",
       Gender: "1",
       NationalityId: "1",
       DivisionId: "1"
     }