之前一直使用微軟自帶的Json,也一直想試著自己解析下Json玩玩,于是花了一個(gè)晚上的時(shí)間寫了個(gè)解析的類,
先說(shuō)下思路,先從簡(jiǎn)單的說(shuō)起:如:標(biāo)準(zhǔn)的JSon格式如:{"Key":"Value1","Key2":1,"Key3":[1,2,3,4],"Key4":{"Test1":"2"},"Key5":[{"A":1,"B":2}]}
我是這樣分的把{}的內(nèi)容看成是一個(gè)完整的對(duì)象,遇到{}就把他當(dāng)完整的對(duì)象解析(我表達(dá)能力可能有問(wèn)題,不太會(huì)表達(dá))
首先是讀Key 嚴(yán)格的格式來(lái)說(shuō) Key必須帶雙引號(hào),但不排除沒(méi)帶的 所以我做了下判斷 如果有雙引號(hào) 就去掉 ,然后都value值,Value值大致可分三種,
一種就是普通的值 如:字符串、數(shù)字、時(shí)間,
第二種是 完整的類 ,
第三種就是數(shù)組,但數(shù)組其實(shí)也可以分為兩種,一種是普通數(shù)組,一種是帶子類的數(shù)組,
測(cè)試了下性能 感覺(jué)還行,就發(fā)上來(lái)了
別吐槽哈 小弟也就這兩下子
1 #region 聲明 2 /******************************************************************************************** 3 * CLR版本:4.0.30319.34011 4 * .NET版本:V4.0 5 * 類 名 稱:SR 6 * 命名空間:DotNet 7 * 創(chuàng)建時(shí)間:2014/3/9 19:19:36 8 * 作 者:中國(guó).NET研究協(xié)會(huì) (網(wǎng)站:http://www.dotnet.org.cn QQ群:45132984) 9 * 識(shí) 別 號(hào):b8f20131-829f-4db0-87a7-d62f8c5ab404 10 ********************************************************************************************/ 11 /*****************************************本版權(quán)權(quán)******************************************* 12 * 本軟件遵照GPL協(xié)議開(kāi)放源代碼,您可以自由傳播和修改,在遵照下面的約束條件的前提下: 13 * 一. 只要你在每一副本上明顯和恰當(dāng)?shù)爻霭姘鏅?quán)聲明,保持此許可證的聲明和沒(méi)有 14 * 擔(dān)保的聲明完整無(wú)損,并和程序一起給每個(gè)其他的程序接受者一份許可證的副本, 15 * 你就可以用任何媒體復(fù)制和發(fā)布你收到的原始的程序的源代碼。你也可以為轉(zhuǎn)讓 16 * 副本的實(shí)際行動(dòng)收取一定費(fèi)用,但必須事先得到的同意。 17 * 二. 你可以修改本軟件的一個(gè)或幾個(gè)副本或程序的任何部分,以此形成基于程序的作品。 18 * 只要你同時(shí)滿足下面的所有條件,你就可以按前面第一款的要求復(fù)制和發(fā)布這一經(jīng) 19 * 過(guò)修改的程序或作品。 20 * 三.只要你遵循一、二條款規(guī)定,您就可以自由使用并傳播本源代碼, 21 * 但必須原封不動(dòng)地保留原作者信息。 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 //函數(shù)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 }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注