JavaScriptSerializer - JSON serialization of enum as string

后端 未结 27 1744
耶瑟儿~
耶瑟儿~ 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:16

    Add the below to your global.asax for JSON serialization of c# enum as string

      HttpConfiguration config = GlobalConfiguration.Configuration;
                config.Formatters.JsonFormatter.SerializerSettings.Formatting =
                    Newtonsoft.Json.Formatting.Indented;
    
                config.Formatters.JsonFormatter.SerializerSettings.Converters.Add
                    (new Newtonsoft.Json.Converters.StringEnumConverter());
    

提交回复
热议问题