欢迎来到我的的博客园,祝大家学有所成,早点实现自己的人生理想。

使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法

一、实体书写

  将枚举类型的属性前面加上[JsonConverter(typeof(StringEnumConverter))]即可。

二、举例

 

[JsonConverter(typeof(StringEnumConverter))]
public CheckTypeEnum checkType { get; set; }
public int num { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public InputTypeEnum type { get; set; }

四、结果

 

另外,如果需要变更属性的名称,则在前面添加[JsonProperty(PropertyName = "newName")]:

        [JsonProperty(PropertyName = "newName")]
        public string key { get; set; }

 

posted @ 2018-03-28 17:21  宋兴柱  阅读(677)  评论(0编辑  收藏  举报