I have a class that contains an enum
property, and upon serializing the object using JavaScriptSerializer
, my json result contains the integer value of the enumeration rather than its string
"name".
(我有一个包含enum
属性的类,并在使用JavaScriptSerializer
序列化对象后,我的json结果包含枚举的整数值,而不是其string
“ name”。)
Is there a way to get the enum as a string
in my json without having to create a custom JavaScriptConverter
? (有没有一种方法可以在我的json中将枚举作为string
获取而无需创建自定义JavaScriptConverter
?)
Perhaps there's an attribute that I could decorate the enum
definition, or object property, with? (也许有一个属性可以修饰enum
定义或对象属性?)
As an example:
(举个例子:)
enum Gender { Male, Female }
class Person
{
int Age { get; set; }
Gender Gender { get; set; }
}
Desired json result:
(所需的json结果:)
{ "Age": 35, "Gender": "Male" }
Ideally looking for answer with built-in .NET framework classes, if not possible alternatives (like Json.net) are welcome.
(理想情况下,使用内置的.NET框架类寻找答案,如果可能的话,欢迎使用替代方法(如Json.net)。)
ask by Omer Bokhari translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…