如何将 JSON 从 Web API 反序列化为 class

How to deserialize JSON from Web API to class

所以我是 JSON 的初学者,我很难将其从网络 API 放入 class。到目前为止,我用邮递员来得到这个:

 {
    "$id": "1",
    "id": 1,
    "name": "Quick Entry",
    "description": "General",
    "trackerNumber": 1,
    "taskType": 0,
    "priority": 1,
    "status": 0,
    "isRecurring": 1,
    "clientVisibility": null,
    "dateCreated": "2015-05-04T11:45:58.867",
    "dateModified": "2017-03-29T11:51:52.007",
    "modifiedBySessionId": "f1a96bf3-7e08-4d44-b8a2-5eacf0adab01",
    "hours": null,
    "deliveryDate": null,
    "discriminator": "",
    "assignedToUserId": null,
    "projectId": 1,
    "clientId": 1,
    "sortOrder": 4,
    "isDeleted": false,
    "folder": "33f16cea-7fb4-e511-80db-00155d001801",
    "taskGroupId": 1,
    "isNew": false
}

"task"是我做的class的名字。这是我背后的代码:

using (var httpClient = new HttpClient { BaseAddress = new Uri("http://localhost:45552/") })
{
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
    var test = await httpClient.GetStringAsync("api/Tasks/1");

    //task = JsonConvert.DeserializeObject<TaskInfo>(test);

    Console.WriteLine(task.Name);
    Console.ReadKey();
}

我只是不确定如何反序列化它。

我的JSONclass:

class TaskInfo
{

    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "trackerNumber")]
    public int TrackerNumber { get; set; }

    [JsonProperty(PropertyName = "taskType")]
    public int TaskType { get; set; }

    [JsonProperty(PropertyName = "priority")]
    public int Priority { get; set; }

    [JsonProperty(PropertyName = "status")]
    public int Status { get; set; }

    [JsonProperty(PropertyName = "isRecurring")]
    public int IsRecurring { get; set; }

    [JsonProperty(PropertyName = "clientVisibility")]
    public int ClientVisibility { get; set; }

    [JsonProperty(PropertyName = "dateCreated")]
    public DateTime DateCreated { get; set; }

    [JsonProperty(PropertyName = "dateModified")]
    public DateTime DateModified { get; set; }

    [JsonProperty(PropertyName = "modifiedBySessionId")]
    public string ModifiedBySessionId { get; set; }

    [JsonProperty(PropertyName = "hours")]
    public int Hours { get; set; }

    [JsonProperty(PropertyName = "deliveryDate")]
    public DateTime DeliveryDate { get; set; }

    [JsonProperty(PropertyName = "discriminator")]
    public string Discriminator { get; set; }

    [JsonProperty(PropertyName = "assignedToUserId")]
    public int AssignedToUserId { get; set; }

    [JsonProperty(PropertyName = "projectId")]
    public int ProjectId { get; set; }

    [JsonProperty(PropertyName = "clientId")]
    public int ClientId { get; set; }

    [JsonProperty(PropertyName = "sortOrder")]
    public int SortOrder { get; set; }

    [JsonProperty(PropertyName = "isDeleted")]
    public bool IsDeleted { get; set; }

    [JsonProperty(PropertyName = "folder")]
    public int Folder { get; set; }

    [JsonProperty(PropertyName = "taskGroupId")]
    public int TaskGroupId { get; set; }

    [JsonProperty(PropertyName = "isNew")]
    public bool IsNew { get; set; }

}

当我尝试打印我的任务时 class,它给我这个错误: "Error converting value {null} to type 'System.Int32'. Path 'clientVisibility', line 1, position 157."

您的问题是,您已在 C# class 中将 ClientVisibility 声明为 int 而不是可为 null 的 int。应该是这样的:

[JsonProperty(PropertyName = "clientVisibility")]
public int? ClientVisibility { get; set; }

在您的 JSON 中,您将 属性 作为 null 传递:

"clientVisibility": null

因此,C# class 属性 必须能够接受空值,如我在上面所示。这同样适用于必须接受 null 的任何其他 int 属性,例如 Hours