Use JavaScriptserializer to process enumerated string serialization
Consider a sample class containing enumeration attributes:
<code class="language-csharp">public class Person { public int Age { get; set; } public Gender Gender { get; set; } }</code>
Question:
<code class="language-json">{ "Age": 35, "Gender": "Male" }</code>
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]
<code class="language-csharp">using Newtonsoft.Json; using Newtonsoft.Json.Converters; public class Person { public int Age { get; set; } [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } }</code>
[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!