ASP.NET JSON(转http://www.360doc.com/content/14/0615/21/18155648_386887590.shtml)
概念介绍
还是先简单说说Json的一些例子吧。注意,以下概念是我自己定义的,可以参考.net里面的TYPE的模型设计
如果有争议,欢迎提出来探讨!
1.最简单:
{"total":0}
total就是值,值是数值,等于0
2. 复杂点
{"total":0,"data":{"377149574" : 1}}
total是值,data是对象,这个对象包含了"377149574"这个值,等于1
3. 最复杂
{"total":0,"data":{"377149574":[{"cid":"377149574"}]}}
total是值,data是对象,377149574是数组,这个数组包含了一些列的对象,例如{"cid":"377149574"}这个对象。
有了以上的概念,就可以设计出通用的json模型了。
万能JSON源码:
using System;
using System.Collections.Generic;
using System.Text;
namespace Pixysoft.Json
{
public class CommonJsonModelAnalyzer
{
protected string _GetKey(string rawjson)
{
if (string.IsNullOrEmpty(rawjson))
return rawjson;
rawjson = rawjson.Trim();
string[] jsons = rawjson.Split(new char[] { ':' });
if (jsons.Length < 2)
return rawjson;
return jsons[0].Replace("\"", "").Trim();
}
protected string _GetValue(string rawjson)
{
if (string.IsNullOrEmpty(rawjson))
return rawjson;
rawjson = rawjson.Trim();
string[] jsons = rawjson.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (jsons.Length < 2)
return rawjson;
StringBuilder builder = new StringBuilder();
for (int i = 1; i < jsons.Length; i++)
{
builder.Append(jsons[i]);
builder.Append(":");
}
if (builder.Length > 0)
builder.Remove(builder.Length - 1, 1);
string value = builder.ToString();
if (value.StartsWith("\""))
value = value.Substring(1);
if (value.EndsWith("\""))
value = value.Substring(0, value.Length - 1);
return value;
}
protected List<string> _GetCollection(string rawjson)
{
//[{},{}]
List<string> list = new List<string>();
if (string.IsNullOrEmpty(rawjson))
return list;
rawjson = rawjson.Trim();
StringBuilder builder = new StringBuilder();
int nestlevel = -1;
int mnestlevel = -1;
for (int i = 0; i < rawjson.Length; i++)
{
if (i == 0)
continue;
else if (i == rawjson.Length - 1)
continue;
char jsonchar = rawjson[i];
if (jsonchar == '{')
{
nestlevel++;
}
if (jsonchar == '}')
{
nestlevel--;
}
if (jsonchar == '[')
{
mnestlevel++;
}
if (jsonchar == ']')
{
mnestlevel--;
}
if (jsonchar == ',' && nestlevel == -1 && mnestlevel == -1)
{
list.Add(builder.ToString());
builder = new StringBuilder();
}
else
{
builder.Append(jsonchar);
}
}
if (builder.Length > 0)
list.Add(builder.ToString());
return list;
}
}
}
using System; namespace Pixysoft.Json private bool isValue = false; private bool isModel = false; private bool isCollection = false; internal CommonJsonModel(string rawjson) if (string.IsNullOrEmpty(rawjson)) rawjson = rawjson.Trim(); if (rawjson.StartsWith("{")) public string Rawjson public bool IsValue() if (string.IsNullOrEmpty(key)) foreach (string subjson in base._GetCollection(this.rawjson)) if (!model.IsValue()) if (model.Key == key) return submodel.IsValue(); return false; if (string.IsNullOrEmpty(key)) foreach (string subjson in base._GetCollection(this.rawjson)) if (!model.IsValue()) if (model.Key == key) return submodel.IsModel(); return false; if (string.IsNullOrEmpty(key)) foreach (string subjson in base._GetCollection(this.rawjson)) if (!model.IsValue()) if (model.Key == key) return submodel.IsCollection(); return false; /// <summary> List<string> list = new List<string>(); foreach (string subjson in base._GetCollection(this.rawjson)) if (!string.IsNullOrEmpty(key)) return list; /// <summary> if (string.IsNullOrEmpty(key)) foreach (string subjson in base._GetCollection(this.rawjson)) if (!model.IsValue()) if (model.Key == key) return null; /// <summary> if (string.IsNullOrEmpty(key)) foreach (string subjson in base._GetCollection(this.rawjson)) if (!model.IsValue()) if (model.Key == key) if (!submodel.IsModel()) return null; /// <summary> if (string.IsNullOrEmpty(key)) foreach (string subjson in base._GetCollection(this.rawjson)) if (!model.IsValue()) if (model.Key == key) if (!submodel.IsCollection()) return null; /// <summary> if (IsValue()) foreach (string subjson in base._GetCollection(rawjson)) return list; /// <summary> return null; return base._GetValue(rawjson);
|
ASP.NET JSON(转http://www.360doc.com/content/14/0615/21/18155648_386887590.shtml)的更多相关文章
- http://www.360doc.com/content/14/0313/17/16070877_360315087.shtml
http://www.360doc.com/content/14/0313/17/16070877_360315087.shtml
- http://www.360doc.com/content/13/0516/22/12094763_285956121.shtml
http://www.360doc.com/content/13/0516/22/12094763_285956121.shtml
- http://www.360doc.com/content/12/1014/00/7471983_241330790.shtml
http://www.360doc.com/content/12/1014/00/7471983_241330790.shtml
- http://www.360doc.com/content/10/1012/09/3722251_60285817.shtml
http://www.360doc.com/content/10/1012/09/3722251_60285817.shtml http://www.docin.com/p-163063250.htm ...
- http://www.360doc.com/content/18/0406/16/15102180_743316618.shtml
http://www.360doc.com/content/18/0406/16/15102180_743316618.shtml
- System.Thread.TImer控件——http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml
http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml
- http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
http://www.360doc.com/content/10/0928/12/11991_57014502.shtml
- C++中的memset()函数 ------------转自:http://www.360doc.com/content/10/1006/18/1704901_58866679.shtml
memset()函数可以对大内存的分配进行很方便的操作(初始化),所谓“初始化”,当然是指将你定义的变量或申请的空间赋予你所期望的值,例如语句int i=0;就表明定义了一个变量i,并初始化为0:如果 ...
- Maven运行JUnit测试(http://www.360doc.com/content/13/0927/15/7304817_317455642.shtml)
Maven单元测试 分类: maven 2012-05-09 15:17 1986人阅读 评论(1) 收藏 举报 maven测试junit单元测试javarandom 目录(?)[-] maven ...
随机推荐
- 彻底卸载tv
1.卸载 2.C:\Program Files (x86),找到teamviewer选项,右击删除 3.开始--输入regedit,打开注册表,找到如下路径:HKEY_LOCAL_MACHINE\SO ...
- MySQL 数据库登录查询
1. 进入到bin目录: 键入cd..,一直到出现C:\ 为止 然后cd bin所在路径: 如: C:\cd C:\Program Files\MySQL\MySQL Server 5.7 ...
- 虚拟机VirtualBox与CentOS 7安装
一.VirtualBox 我们电脑的操作系统一般都是Windows,如果我们要学习Linux,那么可以在我们的电脑上装个虚拟机,然后在这虚拟机上安装Linux.虚拟机可以用VirtualBox 或者 ...
- 【Python全栈-JavaScript】JavaScript入门
JavaScript基础知识点 一.JavaScript概述 参考:http://www.w3school.com.cn/b.asp JavaScript的历史 1.1992年Nombas开发出C-m ...
- SQL数据库中临时表、临时变量和WITH AS关键词创建“临时表”的区别
原文链接:https://www.cnblogs.com/zhaowei303/articles/4204805.html SQL数据库中数据处理时,有时候需要建立临时表,将查询后的结果集放到临时表中 ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
- Python3学习之路~6.5 私有属性和私有方法
属性分为静态属性和动态属性,静态属性就是变量,动态属性就是方法.但是一般我们说的属性就是变量,方法就是方法.私有属性/方法就是外面访问不了,只有自己能够访问的属性/方法.如何将属性和方法分别变成私有属 ...
- git的介绍
1.Git工作区域 2.向仓库中添加文件流程 三.Git初始化及仓库创建和操作 1.Git安装之后需要进行一些基本信息设置 a.设置用户名:git config -- global user.na ...
- 007-js中的Array常用方法
原文:http://www.runoob.com/jsref/jsref-obj-array.html 摘要常用方法 const array=new Array(); // 1 增加元素 // 1.1 ...
- visual tudio 2017--发布