Код: Выделить всё
[
{
"email": "john.doe@sendgrid.com",
"timestamp": 1337966815,
"category": [
"newuser",
"transactional"
],
"event": "open"
},
{
"email": "jane.doe@sendgrid.com",
"timestamp": 1337966815,
"category": "olduser",
"event": "open"
}
]
< /code>
И я хочу покинуть его в список следующего типа: < /p>
class Item
{
public string Email { get; set; }
public int Timestamp { get; set; }
public string Event { get; set; }
public List Category { get; set; }
}
< /code>
Использование следующего кода: < /p>
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var list = JsonSerializer.Deserialize(json, options);
< /code>
Однако, когда я делаю, я получаю следующее исключение: < /p>
System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: > $[1].category | LineNumber: 13 | BytePositionInLine: 25.
at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
< /code>
< /blockquote>
Исключение происходит потому, что значение «категории» < /code> иногда является единственной строкой, а иногда и массивом строк. Как я могу покинуть такое свойство с System.text.json Подробнее здесь: https://stackoverflow.com/questions/594 ... ing-system