C#C
C#3y ago
OkOk

JsonSerializer.Deserialize will not translate number to string

I am trying to deserialize JSON into an object and one of the JSON values is a is sometimes a string and sometimes a number which will not translate into a string property of an object.

Previous forum questions recommend using the attribute JsonNumberHandling to translate possible numbers into strings but it doesn't seem to work in my simple example.

My usage:
public class Extensions
    {
        [JsonNumberHandling(JsonNumberHandling.WriteAsString)]
        public string position { get; set; }
    }


HttpResponseMessage response = await client.GetAsync(urlTjänsteDomän);
PageResponse? page = JsonSerializer.Deserialize<PageResponse>(await response.Content.ReadAsStringAsync());

My error:
System.InvalidOperationException: ''JsonNumberHandlingAttribute' is only valid on a number or a collection of numbers when applied to a property or field. See member 'position' on type 'Confluence.Domain.Info.Generator.Extensions'.'
Was this page helpful?