2017-07-21 2 views
1

deserialisieren, also bin ich ein Anfänger für JSON und ich habe es schwer, es in eine Klasse von einer Web-API zu setzen. Ich habe Postbote benutzt, um das so weit zu bekommen:Wie JSON von Web-API zu Klasse

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

"Aufgabe" ist der Name der Klasse, die ich gemacht habe. Und das ist mein Code hinter:

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

Ich bin nur nicht sicher, wie es zu deserialisieren.

Meine JSON-Klasse:

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

} 

Und wenn ich versuche, meine Aufgabe Klasse zu drucken, es gibt mir diese Fehlermeldung:. „Fehlerwert Umwandlung {null}‚System.Int32‘eingeben Pfad‚clientVisibility ', Zeile 1, Position 157. "

+0

Haben Sie eine Klasse, die Ihren JSON darstellt? – maccettura

+0

ja . – Marq

+2

OK so kommentieren Sie diese Zeile 'task = JsonConvert.DeserializeObject (Test);' Das ist genau, wie Sie Zeichenketten zu einem bekannten Typ deserialize – maccettura

Antwort

2

Ihr Problem ist das, Sie haben ClientVisibility in Ihrer C# -Klasse als int deklariert und nicht als Nullable int. Es sollte so aussehen:

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

In Ihrem JSON Sie die Eigenschaft als null vorbei:

"clientVisibility": null 

Deshalb ist die C# Klasse Eigenschaft muss in der Lage sein, Nullwerte zu akzeptieren, wie ich oben gezeigt habe. Dasselbe gilt auch für alle anderen int Eigenschaften, die null akzeptieren müssen, wie Hours