ASPNET万能JSON解析器Word文档下载推荐.docx
《ASPNET万能JSON解析器Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《ASPNET万能JSON解析器Word文档下载推荐.docx(10页珍藏版)》请在冰豆网上搜索。
0,"
data"
377149574"
:
1}}
total是值,data是对象,这个对象包含了"
这个值,等于1
3.最复杂
[{"
cid"
"
}]}}
total是值,data是对象,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)
returnjsons[0].Replace("
\"
"
).Trim();
}
protectedstring_GetValue(stringrawjson)
},StringSplitOptions.RemoveEmptyEntries);
StringBuilderbuilder=newStringBuilder();
for(inti=1;
i<
jsons.Length;
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<
string>
_GetCollection(stringrawjson)
//[{},{}]
List<
list=newList<
();
returnlist;
intnestlevel=-1;
intmnestlevel=-1;
for(inti=0;
rawjson.Length;
if(i==0)
continue;
elseif(i==rawjson.Length-1)
charjsonchar=rawjson[i];
if(jsonchar=='
{'
)
nestlevel++;
}'
nestlevel--;
['
mnestlevel++;
]'
mnestlevel--;
'
&
&
nestlevel==-1&
mnestlevel==-1)
list.Add(builder.ToString());
builder=newStringBuilder();
else
builder.Append(jsonchar);
}
publicclassCommonJsonModel:
CommonJsonModelAnalyzer
privatestringrawjson;
privateboolisValue=false;
privateboolisModel=false;
privateboolisCollection=false;
internalCommonJsonModel(stringrawjson)
this.rawjson=rawjson;
thrownewException("
missingrawjson"
if(rawjson.StartsWith("
isModel=true;
elseif(rawjson.StartsWith("
["
isCollection=true;
isValue=true;
publicstringRawjson
get{returnrawjson;
publicboolIsValue()
returnisValue;
publicboolIsValue(stringkey)
if(!
isModel)
returnfalse;
if(string.IsNullOrEmpty(key))
foreach(stringsubjsoninbase._GetCollection(this.rawjson))
CommonJsonModelmodel=newCommonJsonModel(subjson);
model.IsValue())
if(model.Key==key)
CommonJsonMo