.NETJson解析到Dictionary,原生代码

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

之前一直使用微软自带的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.340114* .NET版本:V4.05* 类 名 称:SR6* 命名空间:DotNet7* 创建时间:2014/3/9 19:19:368* 作 者:中国.NET研究协会 (网站:http://www.dotnet.org.cnQQ群:45132984)9* 识 别 号:b8f20131-829f-4db0-87a7-d62f8c5ab40410********************************************************************************************/11/*****************************************本版权权*******************************************12* 本软件遵照GPL协议开放源代码,您可以自由传播和修改,在遵照下面的约束条件的前提下:13* 一. 只要你在每一副本上明显和恰当地出版版权声明,保持此许可证的声明和没有14* 担保的声明完整无损,并和程序一起给每个其他的程序接受者一份许可证的副本,15* 你就可以用任何媒体复制和发布你收到的原始的程序的源代码。你也可以为转让16* 副本的实际行动收取一定费用,但必须事先得到的同意。17* 二. 你可以修改本软件的一个或几个副本或程序的任何部分,以此形成基于程序的作品。18* 只要你同时满足下面的所有条件,你就可以按前面第一款的要求复制和发布这一经19* 过修改的程序或作品。20* 三.只要你遵循一、二条款规定,您就可以自由使用并传播本源代码,21* 但必须原封不动地保留原作者信息。22**********************************************************************************************/23usingSystem;24usingSystem.Collections.Generic;25usingSystem.IO;26usingSystem.Linq;27usingSystem.Text;2829namespaceDotNet.Json30{31publicclassJsonReader32{33PRivateTextReader myReader;34privateintEndCount =0;35Dictionary<string,object>myObjDate;36privateJsonReader(TextReader reader,intendCount =0)37{38myObjDate = myObjDate =newDictionary<string,object>();39myReader =reader;40EndCount =endCount;41intintByte =ReadInt();42while(EndCount !=0&& intByte != -1)43{44varkey =ReadKeyName(intByte);45varvalue =ReadValue();46myObjDate.Add(key, value);47if(EndCount ==0) {

break; }48intByte =ReadInt();49}50}51publicJsonReader(TextReader reader)52:this(reader,0)53{ }54publicJsonReader(stringvalue)55:this(newStringReader(value),0)56{ }57privateintReadInt()58{59varintByte =myReader.Read();60if(intByte ==123)61{62EndCount++;63}64elseif(intByte ==125)65{66EndCount--;67}68returnintByte;69}70privatestringReadKeyName(intlastChar)71{72StringBuilder strBuilder =newStringBuilder();73varintByte =lastChar;74if(intByte ==123)75{76intByte =myReader.Read();77}78varlastIntByte = -1;79intendByteInt = -1;80if(intByte == -1)81{82returnnull;83}84if(intByte ==34|| intByte ==39)85{86endByteInt =intByte;87intByte =myReader.Read();88}89while(intByte != -1)90{91if(endByteInt != -1)92{93if(intByte == endByteInt && lastIntByte !=92)94{95ReadInt();96

break;97}98}99elseif(intByte ==58)100{101

break;102}103104strBuilder.Append((char)intByte);105intByte =myReader.Read();106}107returnstrBuilder.ToString();108}109privateobjectReadValue()110{111varintByte =myReader.Read();112if(intByte ==123)113{114//函数115varitem =newJsonReader(myReader,1).myObjDate;116ReadInt();117returnitem;118}119elseif(intByte ==91)120{121returnReadValueArray();122}123else124{125returnReadValueString(intByte);126}127}128privatestringReadValueArrayString(refintlastChar)129{130StringBuilder strBuilder =newStringBuilder();131varintByte =lastChar;132if(intByte ==123)133{134intByte =myReader.Read();135}136varlastIntByte = -1;137intendByteInt = -1;138if(intByte == -1)139{140returnnull;141}142if(intByte ==34|| intByte ==39)143{144endByteInt =intByte;145intByte =myReader.Read();146}147while(intByte != -1)148{149lastChar =intByte;150if(endByteInt != -1)151{152if(intByte == endByteInt && lastIntByte !=92)153{154

break;155}156}157elseif(intByte ==44|| (intByte ==93&& lastIntByte !=92))158{159

break;160}161162strBuilder.Append((char)intByte);163intByte =ReadInt();164}165returnstrBuilder.ToString();166}167privateobjectReadValueString(intlastChar)168{169StringBuilder strBuilder =newStringBuilder();170varintByte =lastChar;171if(intByte ==123)172{173intByte =myReader.Read();174}175varlastIntByte = -1;176intendByteInt = -1;177if(intByte == -1)178{179returnnull;180}181if(intByte ==34|| intByte ==39)182{183endByteInt =intByte;184intByte =myReader.Read();185}186while(intByte != -1)187{188if(endByteInt != -1)189{190if(intByte == endByteInt && lastIntByte !=92)191{192ReadInt();193

break;194}195}196elseif(intByte ==44|| (intByte ==125&& lastIntByte !=92))197{198

break;199}200strBuilder.Append((char)intByte);201intByte =ReadInt();202}203returnstrBuilder.ToString();204}205privateobject[] ReadValueArray()206{207List<object> list =newList<object>();208varintByte =myReader.Read();209while(intByte !=93)210{211if(intByte ==123)212{213varitem =newJsonReader(myReader,1).myObjDate;214list.Add(item);215if(ReadInt() ==93)216{217

break;218}219}220elseif(intByte ==91)221{222list.Add(ReadValueArray());223}224else225{226list.Add(ReadValueArrayString(refintByte));227if(intByte ==93) {

break; }228}229intByte =myReader.Read();230}231returnlist.ToArray();232}233}234}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航