ASPNET万能JSON解析器.docx
《ASPNET万能JSON解析器.docx》由会员分享,可在线阅读,更多相关《ASPNET万能JSON解析器.docx(10页珍藏版)》请在冰豆网上搜索。
![ASPNET万能JSON解析器.docx](https://file1.bdocx.com/fileroot1/2022-11/16/7c7bd0af-0bd0-4375-8237-867263b090ed/7c7bd0af-0bd0-4375-8237-867263b090ed1.gif)
ASPNET万能JSON解析器
ASP.NET万能JSON解析器
概念介绍
还是先简单说说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源码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespacePixysoft.Json
{
publicclassCommonJsonModelAnalyzer
{
protectedstring_GetKey(stringrawjson)
{
if(string.IsNullOrEmpty(rawjson))
returnrawjson;
rawjson=rawjson.Trim();
string[]jsons=rawjson.Split(newchar[]{':
'});
if(jsons.Length<2)
returnrawjson;
returnjsons[0].Replace("\"","").Trim();
}
protectedstring_GetValue(stringrawjson)
{
if(string.IsNullOrEmpty(rawjson))
returnrawjson;
rawjson=rawjson.Trim();
string[]jsons=rawjson.Split(newchar[]{':
'},StringSplitOptions.RemoveEmptyEntries);
if(jsons.Length<2)
returnrawjson;
StringBuilderbuilder=newStringBuilder();
for(inti=1;i {
builder.Append(jsons[i]);
builder.Append(":
");
}
if(builder.Length>0)
builder.Remove(builder.Length-1,1);
stringvalue=builder.ToString();
if(value.StartsWith("\""))
value=value.Substring
(1);
if(value.EndsWith("\""))
value=value.Substring(0,value.Length-1);
returnvalue;
}
protectedList_GetCollection(stringrawjson)
{
//[{},{}]
Listlist=newList();
if(string.IsNullOrEmpty(rawjson))
returnlist;
rawjson=rawjson.Trim();
StringBuilderbuilder=newStringBuilder();
intnestlevel=-1;
intmnestlevel=-1;
for(inti=0;i {
if(i==0)
continue;
elseif(i==rawjson.Length-1)
continue;
charjsonchar=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=newStringBuilder();
}
else
{
builder.Append(jsonchar);
}
}
if(builder.Length>0)
list.Add(builder.ToString());
returnlist;
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespacePixysoft.Json
{
publicclassCommonJsonModel:
CommonJsonModelAnalyzer
{
privatestringrawjson;
privateboolisValue=false;
privateboolisModel=false;
privateboolisCollection=false;
internalCommonJsonModel(stringrawjson)
{
this.rawjson=rawjson;
if(string.IsNullOrEmpty(rawjson))
thrownewException("missingrawjson");
rawjson=rawjson.Trim();
if(rawjson.StartsWith("{"))
{
isModel=true;
}
elseif(rawjson.StartsWith("["))
{
isCollection=true;
}
else
{
isValue=true;
}
}
publicstringRawjson
{
get{returnrawjson;}
}
publicboolIsValue()
{
returnisValue;
}
publicboolIsValue(stringkey)
{
if(!
isModel)
returnfalse;
if(string.IsNullOrEmpty(key))
returnfalse;
foreach(stringsubjsoninbase._GetCollection(this.rawjson))
{
CommonJsonModelmodel=newCommonJsonModel(subjson);
if(!
model.IsValue())
continue;
if(model.Key==key)
{
CommonJsonMo