简体   繁体   中英

C# JSON convert enums to/from strings

There is a question about generically converting enums to/from strings . This questions is a little different.

Is there a way in an ASP.NET Web API 2 app to define the controller API for a call where it will receive/return JSON where the property is a string in the JSON, but it is declared as an enum?

If you are using JSON.NET, you can use the following attribute for your Enum object-

[JsonConverter(typeof(StringEnumConverter))]

Check this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM