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 ...
随机推荐
- LeetCode 748 Shortest Completing Word 解题报告
题目要求 Find the minimum length word from a given dictionary words, which has all the letters from the ...
- 《mongoDB》索引
一:基础操作 创建单列索引 语法: >db.collection.createIndex(keys, options) 语法中 Key 值为你要创建的索引字段,1 为指定按升序创建索引,如果你想 ...
- Struts2验证框架_xml验证失败
测试Struts2验证框架-->XML验证 启动tomcat后网页登录失败 Employee-validation.xml文件头如下: <!DOCTYPE validators PUB ...
- 洛谷P4495 奇怪的背包 [HAOI2018] 数论
正解:数论+dp 解题报告: 传送门! 首先看到这题,跳无数次,自然而然可以想到之前考过好几次了的一个结论——如果只考虑无限放置i,它可以且仅可以跳到gcd(p,v[i]) 举一反三一下,如果有多个i ...
- 使用Apache CXF根据wsdl文件生成代码
1.去官网下载,我用的是apache-cxf-2.5.10.zip 2.解压 3.通过命令行进入Apache CXF的bin目录,如我的目录是D:\BIS\axis2\apache-cxf-2.7.1 ...
- CentOS安装HBase
1.下载HBASE http://www.apache.org/dyn/closer.cgi/hbase/ 2.解压文件到安装目录 #mkdir hbase #cd hbase #tar -zxvf ...
- 让对象支持with语句
一.with语句的好处 with语句的好处在于,它可以自动帮我们释放上下文,就比如文件句柄的操作, 如果你不使用with语句操作,你要先open一个文件句柄,使用完毕后要close这个文件句柄, 而使 ...
- 【托业】托业(TOEIC)成绩 & 等级划分以及评分标准
托业(TOEIC)成绩 & 等级划分以及评分标准 TOEIC全称:Test of English for International Communication(国际交流英语测评) 已通行全球 ...
- Centos安装elasticsearch教程
elasticsearch安装是ytkah在做laravel电商站内搜索要实现的,通过自己的搜索和学习能力不算很费力解决了.下面就整理一下安装elasticsearch教程,服务器是Centos 7, ...
- iOS App让自己的应用在其它应用中打开列表中显示
像百度网盘等应用,里面的文件打开时,都能够通过以下应用再打开文件.以下红色框框内的我的jpg就是我做的一个样例. 由于样例没有提供Icon,所以显示的是默认icon. 以下就是这样例的主要步骤和代 ...