c#——Enum之Json序列化

对象中包含枚举类型,在序列化成Json字符串的时候,显示的是枚举类型对应的数字。

需要在JSON转化的时候做一些操作,使之显示字符串

在枚举类型上添加属性标签

  [JsonConverter(typeof(StringEnumConverter))]

举例如下:

包含枚举类型的对象定义


  [DataContract]
    public class Definition : JsonBase
    {
        public Definition()
        {
            this.Name = string.Empty;
            this.Title = string.Empty;
            this.Description = string.Empty;
            this.Description = string.Empty;
            this.Required = false;
            this.Name = string.Empty;
            this.Title = string.Empty;
        }

        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("description")]
        public string Description { get; set; }
        [JsonProperty("required")]
        public bool Required { get; set; }
        [JsonProperty("type")]
        public Type { get; set; }
        [JsonProperty("format")]
        public string Format { get; set; }
        [JsonProperty("innertag")]
        public bool Innertag { get; set; }
    }

其中Types为枚举类型,定义如下

  public enum Types
    {     //    字符串 ,密封类类型,表示 Unicode 字符串。
        String = 18,
        //    字符串 , 表示一个字符串数组 string[]
        StringArray = 19,
        //     字符串 ,表示字符串的date格式 yyyy-MM-dd HH:mm:ss   如: 2016-09-20 14:53:15
        DateString = 20,
        //     字符串 ,表示空间数据的WKT(well known text)格式
        WKTString = 21,
        //     字符串 ,表示Base64流的字符串格式
        Base64StringArray = 22
    }


以上方式,直接查询数据库,返回结果如下:

 {
      "name": "url",
      "title": "URL",
      "description": "唯一key",
      "required": true,
      "type": 18,
      "format": "bbbbb",
      "innertag": true
    },
    {
      "name": "datasourcename",
      "title": "数据源名称",
      "description": "数据系统名称。",
      "required": true,
      "type": 18,
      "format": "",
      "innertag": true
    }


其中type显示的是枚举类型对应的数值


接下来在枚举类型头部加上标签

   [JsonConverter(typeof(StringEnumConverter))]
    public enum Types
    {     //    字符串 ,密封类类型,表示 Unicode 字符串。
        String = 18,
        //    字符串 , 表示一个字符串数组 string[]
        StringArray = 19,
        //     字符串 ,表示字符串的date格式 yyyy-MM-dd HH:mm:ss   如: 2016-09-20 14:53:15
        DateString = 20,
        //     字符串 ,表示空间数据的WKT(well known text)格式
        WKTString = 21,
        //     字符串 ,表示Base64流的字符串格式
        Base64StringArray = 22
    }

再次查询获得结果如下

 {
      "name": "url",
      "title": "URL",
      "description": "唯一key",
      "required": true,
      "type": "String",
      "format": "bbbbb",
      "innertag": true
    },
    {
      "name": "datasourcename",
      "title": "数据源名称",
      "description": "数据系统名称。",
      "required": true,
      "type": "String",
      "format": "",
      "innertag": true
    }
以上就得到了想要的结果,但是又存在的问题是:

数据库中存储的字符串类型的type字段,如何映射到定义的对象中枚举类型的type

这就需要做转化,把字符串类型转化成types枚举类型的字符串

 Enum.Parse(typeof(Types), value.ToString())

将 String--->Enum的转化


以下附枚举类型的一些转化方法

注意:枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值。

Enum 提供一些实用的静态方法:

(1)比较枚举类的实例的方法

(2)将实例的值转换为其字符串表示形式的方法

(3)将数字的字符串表示形式转换为此类的实例的方法

(4)创建指定枚举和值的实例的方法。

举例:enum Colors { Red, Green, Blue, Yellow };

Enum-->String

(1)利用Object.ToString()方法:如Colors.Green.ToString()的值是"Green"字符串;

(2)利用Enum的静态方法GetName与GetNames:

public static string GetName(Type enumType,Object value)

public static string[] GetNames(Type enumType)

例如:Enum.GetName(typeof(Colors),3))与Enum.GetName(typeof(Colors), Colors.Blue))的值都是"Blue"

Enum.GetNames(typeof(Colors))将返回枚举字符串数组。

String-->Enum

(1)利用Enum的静态方法Parse:

public static Object Parse(Type enumType,string value)

例如:(Colors)Enum.Parse(typeof(Colors), "Red")

Enum-->Int

(1)因为枚举的基类型是除 Char 外的整型,所以可以进行强制转换。

例如:(int)Colors.Red, (byte)Colors.Green

Int-->Enum

(1)可以强制转换将整型转换成枚举类型。

例如:Colors color = (Colors)2 ,那么color即为Colors.Blue

(2)利用Enum的静态方法ToObject。

public static Object ToObject(Type enumType,int value)

例如:Colors color = (Colors)Enum.ToObject(typeof(Colors), 2),那么color即为Colors.Blue

判断某个整型是否定义在枚举中的方法:Enum.IsDefined

public static bool IsDefined(Type enumType,Object value)

例如:Enum.IsDefined(typeof(Colors), n))



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值