简体   繁体   中英

How to deserialize JSON from Web API to class

so I'm a beginner for JSON and I'm having a hard time to put it in a class from a web API. I used postman to get this so far:

 {
    "$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" is the name of the class that I made. And this is my code behind:

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

I'm just not sure how to deserialize it.

My JSON class:

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

}

And when I try to print my task class, it gives me this error: "Error converting value {null} to type 'System.Int32'. Path 'clientVisibility', line 1, position 157."

Your issue is this, you have declared ClientVisibility in you C# class as an int and not a nullable int . It should be like this:

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

In your JSON you passing the property as null:

"clientVisibility": null

Therefore the C# class property must be able to accept null values as I've shown above. The same will also apply to any other int properties that have to accept null such as Hours

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM