JavaScriptSerializer - JSON serialization of enum as string

后端 未结 27 1748
耶瑟儿~
耶瑟儿~ 2020-11-22 03:22

I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer valu

27条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 04:06

    This is easily done by adding a ScriptIgnore attribute to the Gender property, causing it to not be serialised, and adding a GenderString property which does get serialised:

    class Person
    {
        int Age { get; set; }
    
        [ScriptIgnore]
        Gender Gender { get; set; }
    
        string GenderString { get { return Gender.ToString(); } }
    }
    

提交回复
热议问题