I have input payload model (JSON) which contains JSON object by name class
. I have to create a mapping class for JSON but since it contains the restricted keyword class
, I cannot use it.
Below is the JSON to be mapped in the class:
"item": {
"items": [
{
"department": {
"id": "5",
"refName": "test"
},
"class": {
"id": "2",
"refName": "test"
}
}
]
}
I have to use the above payload for the POST operation but since it contains the keyword class
, I cannot use it. How can I achieve this?
2
Answers
class
is a C# reserve keyword. Instead, you should work withJsonPropertyAttribute
(from Newtonsoft.Json) orJsonPropertyName
(from System.Text.Json).For Newtonsoft.Json:
For System.Text.Json:
You also could use
@class
if you don’t want to useJsonProperty
attribute.It can be read here: