enum & json 之间的转换

enum 转为 string:EnumMember & StringEnumConverter

 public enum CampaignStatus : Int32
    {
        [EnumMember(Value = "Running")]
        Running = 1,
        [EnumMember(Value = "Archived")]
        Archived = 2,
        [EnumMember(Value = "NotRunning")]
        NotRunning = 3
    }
        /// 
        /// optional
        /// 
        [JsonProperty("campaignStatus")]
        [JsonConverter(typeof(StringEnumConverter))]
        public CampaignStatus? CampaignStatus { get; set; }

    

enum array 转为string array:StringEnumConverter & JsonProperty

       [JsonProperty(PropertyName = "Status", ItemConverterType = typeof(StringEnumConverter))]
        public CampaignStatus[] Status { get; set; }

 

dll引用:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

  

转载于:https://www.cnblogs.com/panpanwelcome/p/10709272.html

你可能感兴趣的:(enum & json 之间的转换)