You can serialize an enum field in an WebAPI model as a string by adding an attribute:
enum Size
{
Small,
Medium,
Large
}
class Example1
{
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
Size Size { get; set; }
}
This will serialize to this JSON:
{
"Size": "Medium"
}
How can I accomplish the same for a collections of enums?
class Example2
{
IList<Size> Sizes { get; set; }
}
I want to serialize to this JSON:
{
"Sizes":
[
"Medium",
"Large"
]
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…