Consider a sample class containing enumeration attributes:
public class Person { public int Age { get; set; } public Gender Gender { get; set; } }
<:> Question:
{ "Age": 35, "Gender": "Male" }
Using JavaScriptserializer, the gender attribute will be serialized to an integer value, such as "gender": 0 instead of "gender": "male".
<决> Solution:
Use newtonsoft.json newtonsoft.json provides a solution to use the
attribute: The global configuration converter [JsonConverter]
using Newtonsoft.Json; using Newtonsoft.Json.Converters; public class Person { public int Age { get; set; } [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } }
[JsonConverter]
The above is the detailed content of How Can I Serialize Enums as Strings Using JavaScriptSerializer?. For more information, please follow other related articles on the PHP Chinese website!