之前一直使用微软自带的Json,也一直想试着自己解析下Json玩玩,于是花了一个晚上的时间写了个解析的类,

  先说下思路,先从简单的说起:如:标准的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 = ;
Dictionary<string,object> myObjDate;
private JsonReader(TextReader reader, int endCount = )
{
myObjDate = myObjDate = new Dictionary<string, object>();
myReader = reader;
EndCount = endCount;
int intByte = ReadInt();
while (EndCount != && intByte != -)
{
var key = ReadKeyName(intByte);
var value = ReadValue();
myObjDate.Add(key, value);
if (EndCount == ) { break; }
intByte = ReadInt();
}
}
public JsonReader(TextReader reader)
: this(reader, )
{ }
public JsonReader(string value)
: this(new StringReader(value), )
{ }
private int ReadInt()
{
var intByte = myReader.Read();
if (intByte == )
{
EndCount++;
}
else if (intByte == )
{
EndCount--;
}
return intByte;
}
private string ReadKeyName(int lastChar)
{
StringBuilder strBuilder = new StringBuilder();
var intByte = lastChar;
if (intByte == )
{
intByte = myReader.Read();
}
var lastIntByte = -;
int endByteInt = -;
if (intByte == -)
{
return null;
}
if (intByte == || intByte == )
{
endByteInt = intByte;
intByte = myReader.Read();
}
while (intByte != -)
{
if (endByteInt != -)
{
if (intByte == endByteInt && lastIntByte != )
{
ReadInt();
break;
}
}
else if (intByte == )
{
break;
} strBuilder.Append((char)intByte);
intByte = myReader.Read();
}
return strBuilder.ToString();
}
private object ReadValue()
{
var intByte = myReader.Read();
if (intByte == )
{
//函数
var item = new JsonReader(myReader, ).myObjDate;
ReadInt();
return item;
}
else if (intByte == )
{
return ReadValueArray();
}
else
{
return ReadValueString(intByte);
}
}
private string ReadValueArrayString(ref int lastChar)
{
StringBuilder strBuilder = new StringBuilder();
var intByte = lastChar;
if (intByte == )
{
intByte = myReader.Read();
}
var lastIntByte = -;
int endByteInt = -;
if (intByte == -)
{
return null;
}
if (intByte == || intByte == )
{
endByteInt = intByte;
intByte = myReader.Read();
}
while (intByte != -)
{
lastChar = intByte;
if (endByteInt != -)
{
if (intByte == endByteInt && lastIntByte != )
{
break;
}
}
else if (intByte == || (intByte == && lastIntByte != ))
{
break;
} strBuilder.Append((char)intByte);
intByte = ReadInt();
}
return strBuilder.ToString();
}
private object ReadValueString(int lastChar)
{
StringBuilder strBuilder = new StringBuilder();
var intByte = lastChar;
if (intByte == )
{
intByte = myReader.Read();
}
var lastIntByte = -;
int endByteInt = -;
if (intByte == -)
{
return null;
}
if (intByte == || intByte == )
{
endByteInt = intByte;
intByte = myReader.Read();
}
while (intByte != -)
{
if (endByteInt != -)
{
if (intByte == endByteInt && lastIntByte != )
{
ReadInt();
break;
}
}
else if (intByte == || (intByte == && lastIntByte != ))
{
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 != )
{
if (intByte == )
{
var item = new JsonReader(myReader, ).myObjDate;
list.Add(item);
if (ReadInt() == )
{
break;
}
}
else if (intByte == )
{
list.Add(ReadValueArray());
}
else
{
list.Add(ReadValueArrayString(ref intByte));
if (intByte == ) { break; }
}
intByte = myReader.Read();
}
return list.ToArray();
}
}
}

.NET Json 解析到Dictionary,原生代码的更多相关文章

  1. android Json解析详解(详细代码)

    JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  2. 原生JSON解析

    原生JSON解析 JSONObject:JSON数据封装对象JSONArray:JSON数据封装数组 布局: <?xml version="1.0" encoding=&qu ...

  3. Android JSON 解析关键代码

    Android Json 解析其实还是蛮重要的知识点,为什么这么说呢,因为安卓通信大部分的协议都是使用 json 的方式传输,我知道以前大部分是使用的 xml ,但是时代在发展社会在进步,json 成 ...

  4. 几百行代码实现一个 JSON 解析器

    前言 之前在写 gscript时我就在想有没有利用编译原理实现一个更实际工具?毕竟真写一个语言的难度不低,并且也很难真的应用起来. 一次无意间看到有人提起 JSON 解析器,这类工具充斥着我们的日常开 ...

  5. .Net Core 3.0原生Json解析器

    微软官方博客中描述了为什么构造了全新的Json解析器而不是继续使用行业准则Json.Net 微软博客地址:https://devblogs.microsoft.com/dotnet/try-the-n ...

  6. iOS开发之Swift 4 JSON 解析指南

    Apple 终于在 Swift 4 的 Foundation 的模块中添加了对 JSON 解析的原生支持. 虽然已经有很多第三方类库实现了 JSON 解析,但是能够看到这样一个功能强大.易于使用的官方 ...

  7. ios基础篇(二十七)—— Json解析

    一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...

  8. IOS中Json解析的四种方法

    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...

  9. JSON 解析第三方框架

    常见的 JSON 解析第三方框架 JSONKit(最快) SBJson TouchJSON 以上三个框架的性能依次降低! 介绍 JSONKit 第三方框架的目的 JSON 的解析并不是表面上那么简单 ...

随机推荐

  1. Linux 命令 - sort: 行排序文本文件

    命令格式 sort [OPTION]... [FILE]... 命令参数 -b, --ignore-leading-blanks 忽略开头的空白字符. -d, --dictionary-order 只 ...

  2. Linux 命令 - arp: 操作系统的 ARP 缓存

    arp 命令可以查看 ARP 缓存或者手动添加.删除缓存中的条目. 命令格式 arp [-evn] [-H type] [-i if] -a [hostname] arp [-v] [-i if] - ...

  3. 在SQL 2012中使用和Oracle 一样的序列

    使用过Oracle的都知道,Oracle中的自增是靠序列来完成的,在一定程度上蛮方便的.现在SQL 2012中也有序列了.来看看怎么做的吧! SQL Server 现在将序列当成一个对象来实现,创建一 ...

  4. (转)Hessian(C#)介绍及使用说明

    什么是Hessian? Hessian是Caucho开发的一种二进制Web Service协议.支持目前所有流行的开发平台. Hessia能干什么? hessian用来实现web服务. Hessia有 ...

  5. springMVC+Hibernate常用的配置文件

    每次写一个新的web项目时都要写配置文件.比较麻烦,现在把常用到的配置文件记录下来,方便以后使用 web.xml <?xml version="1.0" encoding=& ...

  6. uva439 - Knight Moves(BFS求最短路)

    题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...

  7. HTML5+J2EE实现文件异步上传

    P.S. HTML5经过了W3C的8年努力,终于正式推广了.这次升级最大的就是升级了XMLHTTPRequest,让它变成了XMLHTTPRequest Level II(这有啥奇怪的?).这个对象现 ...

  8. android源码-安卓源码-Android源码下载-安卓游戏源码

    android源码   高仿精仿金山手机卫士应用源码V1.2 高仿精仿金山手机卫士应用源码,该应用的级别实现了金山卫士的级别功能了,可以说跟现实中我们使用的金山卫士应用的功能几乎差不 人气:9286  ...

  9. zabbix短信网关调用问题总结

    在写调用短信网关的shell脚本的时候,发现了一个百思不得其解的问题,用浏览器访问短信接口地址是可以成功接收到短信的.但在shell 里面调用就报错了!!!在反复测试当中发现,在shell 中对特殊字 ...

  10. 转载 C# BindingSource

    1.引言 BindingSource组件是数据源和控件间的一座桥,同时提供了大量的API和Event供我们使用.使用这些API我们可以将Code与各种具体类型数据源进行解耦:使用这些Event我们可以 ...