【问题标题】:How to deserialize JSON from Web API to class如何将 JSON 从 Web API 反序列化为类
【发布时间】:2023-03-28 19:20:01
【问题描述】:

所以我是 JSON 的初学者,我很难将它放入 Web API 的类中。到目前为止,我使用邮递员得到了这个:

 {
    "$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”是我创建的类的名称。这是我背后的代码:

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();
}

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

我的 JSON 类:

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

}

当我尝试打印我的任务类时,它给了我这个错误: “将值 {null} 转换为类型 'System.Int32' 时出错。路径 'clientVisibility',第 1 行,位置 157。”

【问题讨论】:

  • 你有代表你的 JSON 的类吗?
  • .
  • 好的,取消注释这一行 task = JsonConvert.DeserializeObject&lt;TaskInfo&gt;(test); 这正是您将字符串反序列化为已知类型的方式
  • 我尝试这样做并尝试从该课程中打印一些内容,但没有成功。我将为我的 JSON 添加我的类
  • "没用" -- 为什么没用?我们需要具体信息来尝试诊断您遇到的问题

标签: c# json dotnet-httpclient


【解决方案1】:

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

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

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

"clientVisibility": null

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

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 1970-01-01
    • 2012-09-20
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多