c#——Enum之Json序列化

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

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

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

  [JsonConverter(typeof(StringEnumConverter))]

举例如下:

包含枚举类型的对象定义


[csharp]  view plain  copy
 print ?
  1. [DataContract]  
  2.   public class Definition : JsonBase  
  3.   {  
  4.       public Definition()  
  5.       {  
  6.           this.Name = string.Empty;  
  7.           this.Title = string.Empty;  
  8.           this.Description = string.Empty;  
  9.           this.Description = string.Empty;  
  10.           this.Required = false;  
  11.           this.Name = string.Empty;  
  12.           this.Title = string.Empty;  
  13.       }  
  14.   
  15.       [JsonProperty("name")]  
  16.       public string Name { getset; }  
  17.       [JsonProperty("title")]  
  18.       public string Title { getset; }  
  19.       [JsonProperty("description")]  
  20.       public string Description { getset; }  
  21.       [JsonProperty("required")]  
  22.       public bool Required { getset; }  
  23.       [JsonProperty("type")]  
  24.       public Type { getset; }  
  25.       [JsonProperty("format")]  
  26.       public string Format { getset; }  
  27.       [JsonProperty("innertag")]  
  28.       public bool Innertag { getset; }  
  29.   }  

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

[csharp]  view plain  copy
 print ?
  1.   public enum Types  
  2.     {     //    字符串 ,密封类类型,表示 Unicode 字符串。  
  3.         String = 18,  
  4.         //    字符串 , 表示一个字符串数组 string[]  
  5.         StringArray = 19,  
  6.         //     字符串 ,表示字符串的date格式 yyyy-MM-dd HH:mm:ss   如: 2016-09-20 14:53:15  
  7.         DateString = 20,  
  8.         //     字符串 ,表示空间数据的WKT(well known text)格式  
  9.         WKTString = 21,  
  10.         //     字符串 ,表示Base64流的字符串格式  
  11.         Base64StringArray = 22  
  12.     }  


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

[csharp]  view plain  copy
 print ?
  1. {  
  2.      "name""url",  
  3.      "title""URL",  
  4.      "description""唯一key",  
  5.      "required"true,  
  6.      "type": 18,  
  7.      "format""bbbbb",  
  8.      "innertag"true  
  9.    },  
  10.    {  
  11.      "name""datasourcename",  
  12.      "title""数据源名称",  
  13.      "description""数据系统名称。",  
  14.      "required"true,  
  15.      "type": 18,  
  16.      "format""",  
  17.      "innertag"true  
  18.    }  


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


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

[csharp]  view plain  copy
 print ?
  1. [JsonConverter(typeof(StringEnumConverter))]  
  2.  public enum Types  
  3.  {     //    字符串 ,密封类类型,表示 Unicode 字符串。  
  4. sp;       String = 18,  
  5.      //    字符串 , 表示一个字符串数组 string[]  
  6.      StringArray = 19,  
  7.      //     字符串 ,表示字符串的date格式 yyyy-MM-dd HH:mm:ss   如: 2016-09-20 14:53:15  
  8.      DateString = 20,  
  9.      //     字符串 ,表示空间数据的WKT(well known text)格式  
  10.      WKTString = 21,  
  11.      //     字符串 ,表示Base64流的字符串格式  
  12.      Base64StringArray = 22  
  13.  }  

再次查询获得结果如下

[csharp]  view plain  copy
 print ?
  1. {  
  2.      "name""url",  
  3.      "title""URL",  
  4.      "description""唯一key",  
  5.      "required"true,  
  6.      "type""String",  
  7.      "format""bbbbb",  
  8.      "innertag"true  
  9.    },  
  10.    {  
  11.      "name""datasourcename",  
  12.      "title""数据源名称",  
  13.      "description""数据系统名称。",  
  14.      "required"true,  
  15.      "type""String",  
  16.      "format""",  
  17.      "innertag"true  
  18.    }  
以上就得到了想要的结果,但是又存在的问题是:

数据库中存储的字符串类型的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))

猜你喜欢

转载自blog.csdn.net/QingHeShiJiYuan/article/details/78188310