Deserialize json that has some property name starting with a number
JSON data looks like this
[
{
"market_id": "21",
"coin": "DarkCoin",
"code": "DRK",
"exchange": "BTC",
"last_price": "0.01777975",
"yesterday_price": "0.01770278",
"change": "+0.43",
"24hhigh": "0.01800280",
"24hlow": "0.01752015",
"24hvol": "404.202",
"top_bid": "0.01777975",
"top_ask": "0.01790000"
}
]
Notice these 3 properties here 24high, 24hhlow, and 24hvol how do you make a class for that. I need all those properties by the way, not just those 3 properties I mentioned.
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);
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.