简体   繁体   English

C#JSON将枚举与字符串转换

[英]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? ASP.NET Web API 2应用程序中是否可以为调用定义控制器API,该调用将接收/返回JSON,而该属性是JSON中的字符串,但被声明为枚举?

If you are using JSON.NET, you can use the following attribute for your Enum object- 如果您使用的是JSON.NET,则可以将以下属性用于Enum对象-

[JsonConverter(typeof(StringEnumConverter))]

Check this link . 检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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