When i do a GET request to retrieve my list of Persons the enums(Title) get converted into integers:
class Person {
public Title PersonTitle{ get; set;}
public string Name { get; set;}
}
enum Title {
STUDENT,
TEACHER,
DIRECTOR
}
Let’s Say we have the following situation:
Person first = new Person(){
PersonTitle = Title.STUDENT,
Name = "Dave"
}
this wil result in the following JSON:
[
{
"Name" : "Dave",
"PersonTitle" : 1,
}
]
How do I get the real value of the enum (STUDENT in this case) ?
3
Answers
The only code that had to be changed was:
Before :
After :
Now the response looks like:
you can set the JsonConverterAttribute
Updating Enum with EnumMemberAttribute
You need to add the adequate converter to the JSON serializer.
System.Text.Json
:Json.NET
: