JavaScriptSerializer - JSON serialization of enum as string

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

    Asp.Net Core 3 with System.Text.Json

    public void ConfigureServices(IServiceCollection services)
    {
    
        services
            .AddControllers()
            .AddJsonOptions(options => 
               options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())
            );
    
        //...
     }
    
    0 讨论(0)
  • 2020-11-22 04:02

    This version of Stephen's answer doesn't change the name in the JSON:

    [DataContract(
        Namespace = 
           "http://schemas.datacontract.org/2004/07/Whatever")]
    class Person
    {
        [DataMember]
        int Age { get; set; }
    
        Gender Gender { get; set; }
    
        [DataMember(Name = "Gender")]
        string GenderString
        {
            get { return this.Gender.ToString(); }
            set 
            { 
                Gender g; 
                this.Gender = Enum.TryParse(value, true, out g) ? g : Gender.Male; 
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:03

    Just in case anybody finds the above insufficient, I ended up settling with this overload:

    JsonConvert.SerializeObject(objToSerialize, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter())
    
    0 讨论(0)
  • 2020-11-22 04:03
            Person p = new Person();
            p.Age = 35;
            p.Gender = Gender.Male;
            //1.  male="Male";
            string male = Gender.Male.ToString();
    
            p.Gender = Gender.Female;
    
            //2.  female="Female";
            string female = Enum.GetName(typeof(Gender), p.Gender);
    
            JObject jobj = new JObject();
            jobj["Age"] = p.Age;
            jobj["Gender"] = male;
            jobj["Gender2"] = female;
    
            //you result:  josn= {"Age": 35,"Gender": "Male","Gender2": "Female"}
            string json = jobj.ToString();
    
    0 讨论(0)
  • 2020-11-22 04:04

    The combination of Omer Bokhari and uri 's answers is alsways my solution since the values that I want to provide is usually different from what I have in my enum specially that I would like to be able to change my enums if I need to.

    So if anyone is interested, it is something like this:

    public enum Gender
    {
       [EnumMember(Value = "male")] 
       Male,
       [EnumMember(Value = "female")] 
       Female
    }
    
    class Person
    {
        int Age { get; set; }
        [JsonConverter(typeof(StringEnumConverter))]
        Gender Gender { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-22 04:05

    You can create JsonSerializerSettings with the call to JsonConverter.SerializeObject as below:

    var result = JsonConvert.SerializeObject
                (
                    dataObject,
                    new JsonSerializerSettings
                    {
                        Converters = new [] {new StringEnumConverter()}
                    }
                );
    
    0 讨论(0)
提交回复
热议问题