以前一直使用微軟自帶的Json,也一直想試着本身解析下Json玩玩,因而花了一個晚上的時間寫了個解析的類,數組
先說下思路,先從簡單的提及:如:標準的JSon格式如:{"Key":"Value1","Key2":1,"Key3":[1,2,3,4],"Key4":{"Test1":"2"},"Key5":[{"A":1,"B":2}]}函數
我是這樣分的把{}的內容當作是一個完整的對象,遇到{}就把他當完整的對象解析(我表達能力可能有問題,不太會表達)性能
首先是讀Key 嚴格的格式來講 Key必須帶雙引號,但不排除沒帶的 因此我作了下判斷 若是有雙引號 就去掉 ,而後都value值,Value值大體可分三種,測試
一種就是普通的值 如:字符串、數字、時間,網站
第二種是 完整的類 ,ui
第三種就是數組,但數組其實也能夠分爲兩種,一種是普通數組,一種是帶子類的數組,this
測試了下性能 感受還行,就發上來了 spa
別吐槽哈 小弟也就這兩下子開放源代碼
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 * 三.只要你遵循1、二條款規定,您就能夠自由使用並傳播本源代碼, 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 else 124 { 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 else 225 { 226 list.Add(ReadValueArrayString(ref intByte)); 227 if (intByte == 93) { break; } 228 } 229 intByte = myReader.Read(); 230 } 231 return list.ToArray(); 232 } 233 } 234 }