国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

.NETJson解析到Dictionary,原生代碼

2019-11-14 16:40:33
字體:
來源:轉載
供稿:網友

之前一直使用微軟自帶的Json,也一直想試著自己解析下Json玩玩,于是花了一個晚上的時間寫了個解析的類,

 

  先說下思路,先從簡單的說起:如:標準的JSon格式如:{"Key":"Value1","Key2":1,"Key3":[1,2,3,4],"Key4":{"Test1":"2"},"Key5":[{"A":1,"B":2}]}

 我是這樣分的把{}的內容看成是一個完整的對象,遇到{}就把他當完整的對象解析(我表達能力可能有問題,不太會表達)

首先是讀Key 嚴格的格式來說 Key必須帶雙引號,但不排除沒帶的  所以我做了下判斷  如果有雙引號 就去掉 ,然后都value值,Value值大致可分三種,

  一種就是普通的值 如:字符串、數字、時間,

  第二種是 完整的類  ,

  第三種就是數組,但數組其實也可以分為兩種,一種是普通數組,一種是帶子類的數組,

測試了下性能 感覺還行,就發上來了  

別吐槽哈 小弟也就這兩下子

  1 #region 聲明  2 /********************************************************************************************  3      *  CLR版本:4.0.30319.34011  4      * .NET版本:V4.0  5      * 類 名 稱:SR   6      * 命名空間:DotNet  7      * 創建時間:2014/3/9 19:19:36  8      * 作    者:中國.NET研究協會 (網站:http://www.dotnet.org.cn  QQ群:45132984)  9      * 識 別 號:b8f20131-829f-4db0-87a7-d62f8c5ab404 10 ********************************************************************************************/ 11 /*****************************************本版權權******************************************* 12  * 本軟件遵照GPL協議開放源代碼,您可以自由傳播和修改,在遵照下面的約束條件的前提下: 13  *      一. 只要你在每一副本上明顯和恰當地出版版權聲明,保持此許可證的聲明和沒有 14  *          擔保的聲明完整無損,并和程序一起給每個其他的程序接受者一份許可證的副本, 15  *          你就可以用任何媒體復制和發布你收到的原始的程序的源代碼。你也可以為轉讓 16  *          副本的實際行動收取一定費用,但必須事先得到的同意。 17  *      二. 你可以修改本軟件的一個或幾個副本或程序的任何部分,以此形成基于程序的作品。 18  *          只要你同時滿足下面的所有條件,你就可以按前面第一款的要求復制和發布這一經 19  *          過修改的程序或作品。 20  *      三.只要你遵循一、二條款規定,您就可以自由使用并傳播本源代碼, 21  *         但必須原封不動地保留原作者信息。 22  **********************************************************************************************/ 23 using System; 24 using System.Collections.Generic; 25 using System.IO; 26 using System.Linq; 27 using System.Text; 28  29 namespace DotNet.Json 30 { 31     public class JsonReader 32     { 33         PRivate TextReader myReader; 34         private int EndCount = 0; 35         Dictionary<string,object> myObjDate; 36         private JsonReader(TextReader reader, int endCount = 0) 37         { 38             myObjDate = myObjDate = new Dictionary<string, object>(); 39             myReader = reader; 40             EndCount = endCount; 41             int intByte = ReadInt(); 42             while (EndCount != 0 && intByte != -1) 43             { 44                 var key = ReadKeyName(intByte); 45                 var value = ReadValue(); 46                 myObjDate.Add(key, value); 47                 if (EndCount == 0) { break; } 48                 intByte = ReadInt(); 49             } 50         } 51         public JsonReader(TextReader reader) 52             : this(reader, 0) 53         { } 54         public JsonReader(string value) 55             : this(new StringReader(value), 0) 56         { } 57         private int ReadInt() 58         { 59             var intByte = myReader.Read(); 60             if (intByte == 123) 61             { 62                 EndCount++; 63             } 64             else if (intByte == 125) 65             { 66                 EndCount--; 67             } 68             return intByte; 69         } 70         private string ReadKeyName(int lastChar) 71         { 72             StringBuilder strBuilder = new StringBuilder(); 73             var intByte = lastChar; 74             if (intByte == 123) 75             { 76                 intByte = myReader.Read(); 77             } 78             var lastIntByte = -1; 79             int endByteInt = -1; 80             if (intByte == -1) 81             { 82                 return null; 83             } 84             if (intByte == 34 || intByte == 39) 85             { 86                 endByteInt = intByte; 87                 intByte = myReader.Read(); 88             } 89             while (intByte != -1) 90             { 91                 if (endByteInt != -1) 92                 { 93                     if (intByte == endByteInt && lastIntByte != 92) 94                     { 95                         ReadInt(); 96                         break; 97                     } 98                 } 99                 else if (intByte == 58)100                 {101                     break;102                 }103 104                 strBuilder.Append((char)intByte);105                 intByte = myReader.Read();106             }107             return strBuilder.ToString();108         }109         private object ReadValue()110         {111             var intByte = myReader.Read();112             if (intByte == 123)113             {114                 //函數115                 var item = new JsonReader(myReader, 1).myObjDate;116                 ReadInt();117                 return item;118             }119             else if (intByte == 91)120             {121                 return ReadValueArray();122             }123             else124             {125                 return ReadValueString(intByte);126             }127         }128         private string ReadValueArrayString(ref int lastChar)129         {130             StringBuilder strBuilder = new StringBuilder();131             var intByte = lastChar;132             if (intByte == 123)133             {134                 intByte = myReader.Read();135             }136             var lastIntByte = -1;137             int endByteInt = -1;138             if (intByte == -1)139             {140                 return null;141             }142             if (intByte == 34 || intByte == 39)143             {144                 endByteInt = intByte;145                 intByte = myReader.Read();146             }147             while (intByte != -1)148             {149                 lastChar = intByte;150                 if (endByteInt != -1)151                 {152                     if (intByte == endByteInt && lastIntByte != 92)153                     {154                         break;155                     }156                 }157                 else if (intByte == 44 || (intByte == 93 && lastIntByte != 92))158                 {159                     break;160                 }161 162                 strBuilder.Append((char)intByte);163                 intByte = ReadInt();164             }165             return strBuilder.ToString();166         }167         private object ReadValueString(int lastChar)168         {169             StringBuilder strBuilder = new StringBuilder();170             var intByte = lastChar;171             if (intByte == 123)172             {173                 intByte = myReader.Read();174             }175             var lastIntByte = -1;176             int endByteInt = -1;177             if (intByte == -1)178             {179                 return null;180             }181             if (intByte == 34 || intByte == 39)182             {183                 endByteInt = intByte;184                 intByte = myReader.Read();185             }186             while (intByte != -1)187             {188                 if (endByteInt != -1)189                 {190                     if (intByte == endByteInt && lastIntByte != 92)191                     {192                         ReadInt();193                         break;194                     }195                 }196                 else if (intByte == 44 || (intByte == 125 && lastIntByte != 92))197                 {198                     break;199                 }200                 strBuilder.Append((char)intByte);201                 intByte = ReadInt();202             }203             return strBuilder.ToString();204         }205         private object[] ReadValueArray()206         {207             List<object> list = new List<object>();208             var intByte = myReader.Read();209             while (intByte != 93)210             {211                 if (intByte == 123)212                 {213                     var item = new JsonReader(myReader, 1).myObjDate;214                     list.Add(item);215                     if (ReadInt() == 93)216                     {217                         break;218                     }219                 }220                 else if (intByte == 91)221                 {222                     list.Add(ReadValueArray());223                 }224                 else225                 {226                     list.Add(ReadValueArrayString(ref intByte));227                     if (intByte == 93) { break; }228                 }229                 intByte = myReader.Read();230             }231             return list.ToArray();232         }233     }234 }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巫山县| 陵川县| 襄汾县| 威海市| 花莲县| 沧源| 科技| 崇阳县| 张家界市| 德庆县| 昔阳县| 县级市| 临漳县| 普兰县| 滨州市| 八宿县| 凤阳县| 邮箱| 邓州市| 临夏市| 呼和浩特市| 鄂尔多斯市| 安徽省| 巴林右旗| 正阳县| 边坝县| 江源县| 建湖县| 犍为县| 监利县| 竹北市| 涿州市| 曲周县| 枝江市| 洛隆县| 宜兰县| 泸州市| 宁城县| 兴山县| 浦县| 鄂尔多斯市|