1. 程式人生 > >c#——Enum之Json序列化

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))