【问题标题】:Deserialise Json Object in C#code在 C#code 中反序列化 Json 对象
【发布时间】:2014-03-31 19:22:12
【问题描述】:

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

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

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

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

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

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"

它必须在 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 参数为空白(空对象)。 当我将此类型从员工更改为对象时,代码如下所示,我无法将此对象解析为员工类型?

{
  "employee": {
    "EmployeeId": 1,
    "EmployeeCode": "E9584",
    "FirstName": "Carson",
    "Surname": "Alexander",
    "IdNumber": "8704167738384",
    "ContactNo": "0834549632",
    "Gender": 2,
    "PositionId": 1,
    "NationalityId": 1,
    "EmployeeTypeId": 1,
    "DivisionId": 3,
    "Division": {
      "DivisionId": 3,
      "DivisionName": "Garden Court Milpark",
      "Address1": null,
      "Address2": null,
      "Address3": null,
      "PostalCode": null,
      "ContactNo": null,
      "RegionId": 1,
      "EmployeeId": 0,
      "CompanyId": 1,
      "Company": null,
      "Region": null,
      "Employees": [
        {}
      ]
    },
    "Courses": [
      {
        "EmployeeCourseId": 1,
        "EmployeeId": 1,
        "Employee": {},
        "CourseId": 1,
        "Course": {
          "CourseId": 1,
          "CourseCode": "Chem101",
          "CourseDescription": "Chemistry"
        },
        "CourseStatus": "Distinction",
        "CaptureDate": "2014-03-25T21:46:57.093",
        "ExpiryDate": "2015-03-26T21:46:57.09",
        "RegistrationDate": "2014-03-26T21:46:57.093"
      },
      {
        "EmployeeCourseId": 2,
        "EmployeeId": 1,
        "Employee": {},
        "CourseId": 3,
        "Course": {
          "CourseId": 3,
          "CourseCode": "Macro401",
          "CourseDescription": "Macroeconomics"
        },
        "CourseStatus": null,
        "CaptureDate": "2014-03-26T21:46:57.093",
        "ExpiryDate": null,
        "RegistrationDate": null
      },
      {
        "EmployeeCourseId": 3,
        "EmployeeId": 1,
        "Employee": {},
        "CourseId": 4,
        "Course": {
          "CourseId": 4,
          "CourseCode": "cal100",
          "CourseDescription": "Calculus"
        },
        "CourseStatus": null,
        "CaptureDate": "2014-03-26T21:46:57.093",
        "ExpiryDate": null,
        "RegistrationDate": null
      }
    ],
    "Position": null,
    "Nationality": {
      "NationalityId": 1,
      "NationalityDescription": "African",
      "EmployeeId": null
    },
    "EmployeeType": null
  }
}

我希望有人可以帮助并告诉我如何将这个对象放入员工类

谢谢。

【问题讨论】:

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


    【解决方案1】:

    您不能在 MVC 中发布“对象”类型。您需要发布员工类型:

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

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

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

    【讨论】:

    • 感谢您的建议。我尝试了您在上面发布的两个建议,但仍然没有运气。我无法理解的是,如果我将类型设置为对象,我确实会在参数中取回值。然后,当我将其设置为 Employee 类型时,我只得到一个所有属性为空白/空的对象。
    • 嗯,那么问题出在客户发帖上。您需要在 MVC 操作方法中具有强类型参数。你见过 Fiddler 中发布的 JSON 吗?问题可能是其中一个字段没有被正确反序列化。就像“abc”被绑定到一个int。您是否尝试过将 Employee 类更改为只有几个字段?
    • 我现在更改为一个简单的 Employee 类,只有名字和姓氏,当 JSON 到达服务器时,参数仍然为空。我已将代码提交到 GitHub [链接](git@github.com:Ruandv/Training.git) angular_ui 分支。
    【解决方案2】:

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

    感谢所有帮助和建议。

    我将 WebApi 路由更改为:

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

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

    然后在我的 UI 上我将代码更改如下:

    Angular 控制器来自:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多