У меня есть следующие модели:
Код: Выделить всё
public class Contacts
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("paused")]
public bool Paused { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; } = string.Empty;
[JsonPropertyName("owner")]
public bool Owner { get; set; }
[JsonPropertyName("notification_targets")]
public NotificationTargets? NotificationTargets { get; set; }
[JsonPropertyName("teams")]
public List? Teams { get; set; }
}
public class NotificationTargets
{
[JsonPropertyName("email")]
public List? Email { get; set; }
}
public class Email
{
[JsonPropertyName("severity")]
public string Severity { get; set; } = string.Empty;
[JsonPropertyName("address")]
public string Address { get; set; } = string.Empty;
}
public class Teams
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName ("name")]
public string Name { get; set; } = string.Empty;
}
Код: Выделить всё
{
"contacts": [
{
"id": 14960844,
"name": "firstname test",
"paused": false,
"type": "user",
"owner": true,
"notification_targets": {
"email": [
{
"severity": "HIGH",
"address": "firstname@test.co.uk"
}
]
},
"teams": [
{
"id": 804736,
"name": "Team 1"
}
]
}
]
}
И я просто вызываю:
Код: Выделить всё
var myContacts = JsonSerializer.Deserialize(myJsonString);
System.Text.Json.JsonException: 'Значение JSON может не конвертироваться
в System.Collections.Generic.List`1[VTMShared.Models.Contacts]. Путь:
$ | НомерЛинии: 0 | BytePositionInLine: 1.'
Я действительно не понимаю, что в этом плохого.
Подробнее здесь: https://stackoverflow.com/questions/790 ... erializing