.NET Json 解析到Dictionary,原生代码
2014-05-06 16:00
309 查看
之前一直使用微软自带的Json,也一直想试着自己解析下Json玩玩,于是花了一个晚上的时间写了个解析的类,
先说下思路,先从简单的说起:如:标准的JSon格式如:{"Key":"Value1","Key2":1,"Key3":[1,2,3,4],"Key4":{"Test1":"2"},"Key5":[{"A":1,"B":2}]}
我是这样分的把{}的内容看成是一个完整的对象,遇到{}就把他当完整的对象解析(我表达能力可能有问题,不太会表达)
首先是读Key 严格的格式来说 Key必须带双引号,但不排除没带的 所以我做了下判断 如果有双引号 就去掉 ,然后都value值,Value值大致可分三种,
一种就是普通的值 如:字符串、数字、时间,
第二种是 完整的类 ,
第三种就是数组,但数组其实也可以分为两种,一种是普通数组,一种是带子类的数组,
测试了下性能 感觉还行,就发上来了
别吐槽哈 小弟也就这两下子
先说下思路,先从简单的说起:如:标准的JSon格式如:{"Key":"Value1","Key2":1,"Key3":[1,2,3,4],"Key4":{"Test1":"2"},"Key5":[{"A":1,"B":2}]}
我是这样分的把{}的内容看成是一个完整的对象,遇到{}就把他当完整的对象解析(我表达能力可能有问题,不太会表达)
首先是读Key 严格的格式来说 Key必须带双引号,但不排除没带的 所以我做了下判断 如果有双引号 就去掉 ,然后都value值,Value值大致可分三种,
一种就是普通的值 如:字符串、数字、时间,
第二种是 完整的类 ,
第三种就是数组,但数组其实也可以分为两种,一种是普通数组,一种是带子类的数组,
测试了下性能 感觉还行,就发上来了
别吐槽哈 小弟也就这两下子
#region 声明 /******************************************************************************************** * CLR版本:4.0.30319.34011 * .NET版本:V4.0 * 类 名 称:SR * 命名空间:DotNet * 创建时间:2014/3/9 19:19:36 * 作 者:中国.NET研究协会 (网站:http://www.dotnet.org.cn QQ群:45132984) * 识 别 号:b8f20131-829f-4db0-87a7-d62f8c5ab404 ********************************************************************************************/ /*****************************************本版权权******************************************* * 本软件遵照GPL协议开放源代码,您可以自由传播和修改,在遵照下面的约束条件的前提下: * 一. 只要你在每一副本上明显和恰当地出版版权声明,保持此许可证的声明和没有 * 担保的声明完整无损,并和程序一起给每个其他的程序接受者一份许可证的副本, * 你就可以用任何媒体复制和发布你收到的原始的程序的源代码。你也可以为转让 * 副本的实际行动收取一定费用,但必须事先得到的同意。 * 二. 你可以修改本软件的一个或几个副本或程序的任何部分,以此形成基于程序的作品。 * 只要你同时满足下面的所有条件,你就可以按前面第一款的要求复制和发布这一经 * 过修改的程序或作品。 * 三.只要你遵循一、二条款规定,您就可以自由使用并传播本源代码, * 但必须原封不动地保留原作者信息。 **********************************************************************************************/ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace DotNet.Json { public class JsonReader { private TextReader myReader; private int EndCount = 0; Dictionary<string,object> myObjDate; private JsonReader(TextReader reader, int endCount = 0) { myObjDate = myObjDate = new Dictionary<string, object>(); myReader = reader; EndCount = endCount; int intByte = ReadInt(); while (EndCount != 0 && intByte != -1) { var key = ReadKeyName(intByte); var value = ReadValue(); myObjDate.Add(key, value); if (EndCount == 0) { break; } intByte = ReadInt(); } } public JsonReader(TextReader reader) : this(reader, 0) { } public JsonReader(string value) : this(new StringReader(value), 0) { } private int ReadInt() { var intByte = myReader.Read(); if (intByte == 123) { EndCount++; } else if (intByte == 125) { EndCount--; } return intByte; } private string ReadKeyName(int lastChar) { StringBuilder strBuilder = new StringBuilder(); var intByte = lastChar; if (intByte == 123) { intByte = myReader.Read(); } var lastIntByte = -1; int endByteInt = -1; if (intByte == -1) { return null; } if (intByte == 34 || intByte == 39) { endByteInt = intByte; intByte = myReader.Read(); } while (intByte != -1) { if (endByteInt != -1) { if (intByte == endByteInt && lastIntByte != 92) { ReadInt(); break; } } else if (intByte == 58) { break; } strBuilder.Append((char)intByte); intByte = myReader.Read(); } return strBuilder.ToString(); } private object ReadValue() { var intByte = myReader.Read(); if (intByte == 123) { //函数 var item = new JsonReader(myReader, 1).myObjDate; ReadInt(); return item; } else if (intByte == 91) { return ReadValueArray(); } else { return ReadValueString(intByte); } } private string ReadValueArrayString(ref int lastChar) { StringBuilder strBuilder = new StringBuilder(); var intByte = lastChar; if (intByte == 123) { intByte = myReader.Read(); } var lastIntByte = -1; int endByteInt = -1; if (intByte == -1) { return null; } if (intByte == 34 || intByte == 39) { endByteInt = intByte; intByte = myReader.Read(); } while (intByte != -1) { lastChar = intByte; if (endByteInt != -1) { if (intByte == endByteInt && lastIntByte != 92) { break; } } else if (intByte == 44 || (intByte == 93 && lastIntByte != 92)) { break; } strBuilder.Append((char)intByte); intByte = ReadInt(); } return strBuilder.ToString(); } private object ReadValueString(int lastChar) { StringBuilder strBuilder = new StringBuilder(); var intByte = lastChar; if (intByte == 123) { intByte = myReader.Read(); } var lastIntByte = -1; int endByteInt = -1; if (intByte == -1) { return null; } if (intByte == 34 || intByte == 39) { endByteInt = intByte; intByte = myReader.Read(); } while (intByte != -1) { if (endByteInt != -1) { if (intByte == endByteInt && lastIntByte != 92) { ReadInt(); break; } } else if (intByte == 44 || (intByte == 125 && lastIntByte != 92)) { break; } strBuilder.Append((char)intByte); intByte = ReadInt(); } return strBuilder.ToString(); } private object[] ReadValueArray() { List<object> list = new List<object>(); var intByte = myReader.Read(); while (intByte != 93) { if (intByte == 123) { var item = new JsonReader(myReader, 1).myObjDate; list.Add(item); if (ReadInt() == 93) { break; } } else if (intByte == 91) { list.Add(ReadValueArray()); } else { list.Add(ReadValueArrayString(ref intByte)); if (intByte == 93) { break; } } intByte = myReader.Read(); } return list.ToArray(); } } }
相关文章推荐
- 微信公众平台开发之语音识别.Net代码解析
- android Json解析详解(详细代码)
- IOS开发专题---转化解析JSON格式为字符格式(原生类库解析)
- .NET中常用的几种解析JSON方法
- Android json数据解析详解及实例代码
- Json 生成与解析详解及实例代码
- [Android | 小代码] 天气预报 简易Demo json解析数组+对象 Gson+Volley简单使用
- Ajax解析html、xml、json、js原生方式、jquery方式
- android客户端从服务器端获取json数据并解析的实现代码get+post
- JSON数据解析(一)——原生方法
- JSON语句解析代码
- Android JSON解析示例代码
- 用安卓自带的原生方法解析从webservice获取的json数据
- 第一行代码 第九章 网络技术 - 解析JSON格式数据
- JSON解析--原生&AFN
- [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析
- .net使用Newtonsoft.Json.dll解析json过程的几种特殊情况处理
- 数据Json解析原生
- Android--使用原生技术实现ListView(原生技术实现网络异步请求,解析json数据)
- android客户端从服务器端获取json数据并解析的实现代码