c#——Enum之Json序列化
象中包含列舉型別,在序列化成Json字串的時候,顯示的是列舉型別對應的數字。
需要在JSON轉化的時候做一些操作,使之顯示字串
在列舉型別上新增屬性標籤
[JsonConverter(typeof(StringEnumConverter))]
舉例如下:
包含列舉型別的物件定義
[csharp] view plain copy print ?
- [DataContract]
- public class Definition : JsonBase
- {
- public Definition()
- {
- this
- this.Title = string.Empty;
- this.Description = string.Empty;
- this
- 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為列舉型別,定義如下
[csharp] view plain copy print ?
- 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
- }
[csharp] view plain copy print ?
- {
- "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
- }
接下來在列舉型別頭部加上標籤
[csharp] view plain copy print ?
- [JsonConverter(typeof(StringEnumConverter))]
- public enum Types
- { // 字串 ,密封類型別,表示 Unicode 字串。
- sp; 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
- }
再次查詢獲得結果如下
[csharp] view plain copy print ?
- {
- "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))