I have input payload model (JSON) which contains json-object by name “class”. I have to create mapping class for JSON but since it contains C# restricted keyword “class”, I cannot use.
below is JSON to be mapped in C# class –
"item": {
"items": [
{
"department": {
"id": "5",
"refName": "test"
},
"class": {
"id": "2",
"refName": "test"
}
}]}
I have to use above payload for post operation but since it contains keyword “class”, I cannot use. how can I achieve this?
3
class
is C# reserve keyword. Instead, you should work with JsonPropertyAttribute
(from Newtonsoft.Json) or JsonPropertyName
(from System.Text.Json).
public class Item2
{
[JsonProperty("department")]
public Department Department { get; set; }
[JsonProperty("class")] // For Newtonsoft.Json
public Class Class { get; set; }
//[JsonPropertyName("class")] // For System.Text.Json
//public Class Class { get; set; }
}
public class Class
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("refName")]
public string RefName { get; set; }
}