Deserialize json that has some property name starting with a number

For .NET Core 3.0 and beyond, you can now use the System.Text.Json namespace. If you are using this:

public class MyClass
{
    ...
    [JsonPropertyName("24hhigh")]
    public string twentyFourhhigh { get; set; }
    ...
}

You can use JsonPropertyName Attribute.


You should use JSON.NET or similar library that offers some more advanced options of deserialization. With JSON.NET all you need is adding JsonProperty attribute and specify its custom name that appears in resulting JSON. Here is the example:

   public class MyClass
   {
        [JsonProperty(PropertyName = "24hhigh")]
        public string Highest { get; set; }
        ...

Now to deserialize:

    string jsonData = ...    
    MyClass deserializedMyClass = JsonConvert.DeserializeObject<MyClass>(jsonData);

Tags:

C#

Json.Net