日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
也談C#之Json,從Json字符串到類代碼

json轉(zhuǎn)類對象

目前創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、北票網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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

1、添加引用 System.Web.Extensions

  

2、測試一下代碼

 
 
  1. static class Program 
  2.     { 
  3.         ///  
  4.         /// 程序的主入口點。 
  5.         ///  
  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(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 { get; set; } 
  22.  
  23.             public int age { get; set; } 
  24.  
  25.             public List likes { get; set; } 
  26.         } 
  27.     } 

輸出內(nèi)容:

逆思考

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

于是LZ思前,向后,想到了以前用過的一個微軟的類庫,應(yīng)該是微軟的一個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對象,內(nèi)部也可以繼續(xù)進(jìn)行細(xì)分,于是測試了種種,稍后會上源碼。先看測試效果吧。

我們隨便在web上面找了一個json字符串來進(jìn)行處理。當(dāng)然json要稍稍復(fù)雜一點。

  

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.     ///  
  9.     /// Json字符串zhuanh 
  10.     ///  
  11.     public class JsonHelper : IHelper 
  12.     { 
  13.         ///  
  14.         /// 是否添加get set 
  15.         ///  
  16.         private bool isAddGetSet = false; 
  17.  
  18.         ///  
  19.         /// 數(shù)據(jù)集合,臨時 
  20.         ///  
  21.         private List dataList = new List(); 
  22.  
  23.         public JsonHelper() 
  24.         { 
  25.         } 
  26.  
  27.         public JsonHelper(bool isAddGetSet) 
  28.         { 
  29.             this.isAddGetSet = isAddGetSet; 
  30.         } 
  31.  
  32.         ///  
  33.         /// 獲取類的字符串形式 
  34.         ///  
  35.         ///  
  36.         ///  
  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.         ///  
  73.         /// 獲取類型的字符串表示 
  74.         ///  
  75.         ///  
  76.         ///  
  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)) 
  96.             { 
  97.                 return "List"; 
  98.             } 
  99.             else if (type == typeof(List)) 
  100.             { 
  101.                 return "List"; 
  102.             } 
  103.             else 
  104.             { 
  105.                 return "string"; 
  106.             } 
  107.         } 
  108.  
  109.         ///  
  110.         /// 獲取字典類型 
  111.         ///  
  112.         ///  
  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.         ///  
  153.         /// 讀取集合類型 
  154.         ///  
  155.         ///  
  156.         ///  
  157.         ///  
  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 { get; set; } 
  182.  
  183.         private Dictionary dic = new Dictionary(); 
  184.  
  185.         public Dictionary Dic 
  186.         { 
  187.             get 
  188.             { 
  189.                 return this.dic; 
  190.             } 
  191.             set 
  192.             { 
  193.                 this.dic = value; 
  194.             } 
  195.         } 
  196.     } 

 調(diào)用方式:

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

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

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


網(wǎng)站標(biāo)題:也談C#之Json,從Json字符串到類代碼
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/djogojd.html