自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

也談C#之Json,從Json字符串到類代碼

開發(fā) 后端 開發(fā)工具
自從.net 4.0開始,微軟提供了一整套的針對json進行處理的方案。其中,就有如何把json字符串轉化成C#類對象,其實這段代碼很多人都清楚,大家也都認識,我就不多說,先貼代碼。

json轉類對象

自從.net 4.0開始,微軟提供了一整套的針對json進行處理的方案。其中,就有如何把json字符串轉化成C#類對象,其實這段代碼很多人都清楚,大家也都認識,我就不多說,先貼代碼。

1、添加引用 System.Web.Extensions

  

2、測試一下代碼

  1. static class Program 
  2.     { 
  3.         /// <summary> 
  4.         /// 程序的主入口點。 
  5.         /// </summary> 
  6.         static void Main() 
  7.         { 
  8.             string jsonStr = "{\"name\":\"supperlitt\",\"age\":25,\"likes\":[\"C#\",\"asp.net\"]}"
  9.             JavaScriptSerializer js = new JavaScriptSerializer(); 
  10.             var model = js.Deserialize<TestModel>(jsonStr); 
  11.  
  12.             Console.WriteLine(model.name); 
  13.             Console.WriteLine(model.age); 
  14.             Console.WriteLine(string.Join(",", model.likes)); 
  15.  
  16.             Console.ReadLine(); 
  17.         } 
  18.  
  19.         public class TestModel 
  20.         { 
  21.             public string name { getset; } 
  22.  
  23.             public int age { getset; } 
  24.  
  25.             public List<string> likes { getset; } 
  26.         } 
  27.     } 

輸出內容:

 

逆思考

由于代碼中,經(jīng)常會遇到需要處理json字符串(抓包比較頻繁)。每次遇到json字符串,大多需要解析,又要進行重復勞動,又需要定義一個C#對象類,有沒有一個比較好的辦法解決呢,不用每次都去寫代碼。自動生成多好。。。

于是LZ思前,向后,想到了以前用過的一個微軟的類庫,應該是微軟的一個Com庫。

      

#p#

 從json字符串自動生成C#類

1、試著百度了一下,也嘗試了幾個可以使用的類。于是找到了

如下的代碼,能夠解析一個json字符串,成為一個C#的對象。

這里引用了,Microsoft.JScript.dll 類庫。

 

  1. Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine(); 
  2. var m = Microsoft.JScript.Eval.JScriptEvaluate("(" + jsonStr + ")", ve); 

 2、發(fā)現(xiàn)這個m對象,其實是一個JSObject對象,內部也可以繼續(xù)進行細分,于是測試了種種,稍后會上源碼。先看測試效果吧。

我們隨便在web上面找了一個json字符串來進行處理。當然json要稍稍復雜一點。

  

ps:代碼如下

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using Microsoft.JScript; 
  6.  
  7. namespace Common 
  8.     /// <summary> 
  9.     /// Json字符串zhuanh 
  10.     /// </summary> 
  11.     public class JsonHelper : IHelper 
  12.     { 
  13.         /// <summary> 
  14.         /// 是否添加get set 
  15.         /// </summary> 
  16.         private bool isAddGetSet = false
  17.  
  18.         /// <summary> 
  19.         /// 數(shù)據(jù)集合,臨時 
  20.         /// </summary> 
  21.         private List<AutoClass> dataList = new List<AutoClass>(); 
  22.  
  23.         public JsonHelper() 
  24.         { 
  25.         } 
  26.  
  27.         public JsonHelper(bool isAddGetSet) 
  28.         { 
  29.             this.isAddGetSet = isAddGetSet; 
  30.         } 
  31.  
  32.         /// <summary> 
  33.         /// 獲取類的字符串形式 
  34.         /// </summary> 
  35.         /// <param name="jsonStr"></param> 
  36.         /// <returns></returns> 
  37.         public string GetClassString(string jsonStr) 
  38.         { 
  39.             Microsoft.JScript.Vsa.VsaEngine ve = Microsoft.JScript.Vsa.VsaEngine.CreateEngine(); 
  40.             var m = Microsoft.JScript.Eval.JScriptEvaluate("(" + jsonStr + ")", ve); 
  41.  
  42.             int index = 0; 
  43.             var result = GetDicType((JSObject)m, ref index); 
  44.  
  45.             StringBuilder content = new StringBuilder(); 
  46.             foreach (var item in dataList) 
  47.             { 
  48.                 content.AppendFormat("\tpublic class {0}\r\n", item.CLassName); 
  49.                 content.AppendLine("\t{"); 
  50.                 foreach (var model in item.Dic) 
  51.                 { 
  52.                     if (isAddGetSet) 
  53.                     { 
  54.                         content.AppendFormat("\t\tpublic {0} {1}", model.Value, model.Key); 
  55.                         content.Append(" { get; set; }\r\n"); 
  56.                     } 
  57.                     else 
  58.                     { 
  59.                         content.AppendFormat("\t\tpublic {0} {1};\r\n", model.Value, model.Key); 
  60.                     } 
  61.  
  62.                     content.AppendLine(); 
  63.                 } 
  64.  
  65.                 content.AppendLine("\t}"); 
  66.                 content.AppendLine(); 
  67.             } 
  68.  
  69.             return content.ToString(); 
  70.         } 
  71.  
  72.         /// <summary> 
  73.         /// 獲取類型的字符串表示 
  74.         /// </summary> 
  75.         /// <param name="type"></param> 
  76.         /// <returns></returns> 
  77.         private string GetTypeString(Type type) 
  78.         { 
  79.             if (type == typeof(int)) 
  80.             { 
  81.                 return "int"
  82.             } 
  83.             else if (type == typeof(bool)) 
  84.             { 
  85.                 return "bool"
  86.             } 
  87.             else if (type == typeof(Int64)) 
  88.             { 
  89.                 return "long"
  90.             } 
  91.             else if (type == typeof(string)) 
  92.             { 
  93.                 return "string"
  94.             } 
  95.             else if (type == typeof(List<string>)) 
  96.             { 
  97.                 return "List<string>"
  98.             } 
  99.             else if (type == typeof(List<int>)) 
  100.             { 
  101.                 return "List<int>"
  102.             } 
  103.             else 
  104.             { 
  105.                 return "string"
  106.             } 
  107.         } 
  108.  
  109.         /// <summary> 
  110.         /// 獲取字典類型 
  111.         /// </summary> 
  112.         /// <returns></returns> 
  113.         private string GetDicType(JSObject jsObj, ref int index) 
  114.         { 
  115.             AutoClass classInfo = new AutoClass(); 
  116.  
  117.             var model = ((Microsoft.JScript.JSObject)(jsObj)).GetMembers(System.Reflection.BindingFlags.GetField); 
  118.             foreach (Microsoft.JScript.JSField item in model) 
  119.             { 
  120.                 string name = item.Name; 
  121.                 Type type = item.GetValue(item).GetType(); 
  122.                 if (type == typeof(ArrayObject)) 
  123.                 { 
  124.                     // 集合 
  125.                     string typeName = GetDicListType((ArrayObject)item.GetValue(item), ref index); 
  126.                     if (!string.IsNullOrEmpty(typeName)) 
  127.                     { 
  128.                         classInfo.Dic.Add(name, typeName); 
  129.                     } 
  130.                 } 
  131.                 else if (type == typeof(JSObject)) 
  132.                 { 
  133.                     // 單個對象 
  134.                     string typeName = GetDicType((JSObject)item.GetValue(item), ref index); 
  135.                     if (!string.IsNullOrEmpty(typeName)) 
  136.                     { 
  137.                         classInfo.Dic.Add(name, typeName); 
  138.                     } 
  139.                 } 
  140.                 else 
  141.                 { 
  142.                     classInfo.Dic.Add(name, GetTypeString(type)); 
  143.                 } 
  144.             } 
  145.  
  146.             index++; 
  147.             classInfo.CLassName = "Class" + index; 
  148.             dataList.Add(classInfo); 
  149.             return classInfo.CLassName; 
  150.         } 
  151.  
  152.         /// <summary> 
  153.         /// 讀取集合類型 
  154.         /// </summary> 
  155.         /// <param name="jsArray"></param> 
  156.         /// <param name="index"></param> 
  157.         /// <returns></returns> 
  158.         private string GetDicListType(ArrayObject jsArray, ref int index) 
  159.         { 
  160.             string name = string.Empty; 
  161.             if ((int)jsArray.length > 0) 
  162.             { 
  163.                 var item = jsArray[0]; 
  164.                 var type = item.GetType(); 
  165.                 if (type == typeof(JSObject)) 
  166.                 { 
  167.                     name = "List<" + GetDicType((JSObject)item, ref index) + ">"
  168.                 } 
  169.                 else 
  170.                 { 
  171.                     name = "List<" + GetTypeString(type) + ">"
  172.                 } 
  173.             } 
  174.  
  175.             return name; 
  176.         } 
  177.     } 
  178.  
  179.     public class AutoClass 
  180.     { 
  181.         public string CLassName { getset; } 
  182.  
  183.         private Dictionary<stringstring> dic = new Dictionary<stringstring>(); 
  184.  
  185.         public Dictionary<stringstring> Dic 
  186.         { 
  187.             get 
  188.             { 
  189.                 return this.dic; 
  190.             } 
  191.             set 
  192.             { 
  193.                 this.dic = value; 
  194.             } 
  195.         } 
  196.     } 

 調用方式:

 

  1. JsonHelper helper = new JsonHelper(true); 
  2. try 
  3.    this.txtOutPut.Text = helper.GetClassString("json字符串"); 
  4. catch 
  5.    this.txtOutPut.Text = "輸入內容不符合規(guī)范..."
  6. }

 ***如果dudu允許的話,我再附上一個測試地址吧:http://www.51debug.com/tool/JsonToCharpCode.aspx

 博客也寫了幾次了,不過每次都寫得比較濫,看著不舒服,這次用心寫了一下,歡迎大家拍磚或提供更好的建議。

責任編輯:王雪燕 來源: 博客園
相關推薦

2013-07-23 06:39:49

Json字符串到JsoAndroid開發(fā)學習Json萬能解析器

2009-08-07 14:46:59

C#匹配字符串

2009-08-26 13:24:54

C#字符串

2009-08-24 17:06:37

C#字符串

2009-08-07 14:15:21

C#字符串分割

2009-08-07 14:22:56

C#字符串搜索

2009-08-07 14:34:33

C#模式字符串

2009-08-24 13:04:44

操作步驟C#字符串

2009-08-07 13:50:11

C#字符串

2009-08-06 16:01:09

C#字符串函數(shù)大全

2009-09-02 17:44:41

C#字符串處理

2009-08-28 10:39:37

C#數(shù)值字符串

2009-08-07 15:58:54

C#字符串插入html

2009-09-02 16:21:20

C#字符串

2009-08-20 18:23:29

C#中SQL連接字符串

2023-05-05 07:49:07

GolangJSON格式

2013-06-17 15:41:09

Windows PhoWP開發(fā)JSON生成C#類

2009-08-07 14:02:12

C#數(shù)據(jù)庫連接字符串

2009-08-21 15:06:09

C#連接字符串

2009-08-11 10:26:49

C#算法C#字符串反轉
點贊
收藏

51CTO技術棧公眾號