JSON帮助类cs.docx

上传人:b****3 文档编号:5222639 上传时间:2022-12-14 格式:DOCX 页数:24 大小:19.19KB
下载 相关 举报
JSON帮助类cs.docx_第1页
第1页 / 共24页
JSON帮助类cs.docx_第2页
第2页 / 共24页
JSON帮助类cs.docx_第3页
第3页 / 共24页
JSON帮助类cs.docx_第4页
第4页 / 共24页
JSON帮助类cs.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

JSON帮助类cs.docx

《JSON帮助类cs.docx》由会员分享,可在线阅读,更多相关《JSON帮助类cs.docx(24页珍藏版)》请在冰豆网上搜索。

JSON帮助类cs.docx

JSON帮助类cs

//用于构建属性值的回调

publicdelegatevoidSetPropertiesDelegate(JsonObjectproperty);

//JsonObject属性值类型

publicenumJsonPropertyType

{

String,

Object,

Array,

Number,

Bool,

Null

}

//JSON通用对象

publicclassJsonObject

{

privateDictionarymProperty;

//创建一个JSON对象

publicJsonObject()

{

this.mProperty=null;

}

//通过字符串构造一个JSON对象

publicJsonObject(SetPropertiesDelegatecallbck)

{

if(callback!

=null)

{

callback(this);

}

}

//Json字符串解析

privatevoidParse(refStringjsonString)

{

intlen=jsonString.Length;

if(String.IsNullOrEmpty(jsonString)||jsonString.Substring(0,1)!

="{"||jsonString.Substring(jsonString.Length-1,1)!

="}")

{

thrownewArgumentException("传入文本不符合Json格式"+jsonString);

}

Stackstack=newStack();

StackstackType=newStack();

StackstackStrValue=newStack();//Value中的特殊字符需要保留

StringBuildersb=newStringBuilder();

Charcur;

boolisValue=false;

JsonPropertylast=null;

for(inti=1;i<=len-2;i++)

{

cur=jsonString[i];

if(cur=='}')

{

;

}

if(cur==''&&stack.Count==0&&stackStrValue.Count==0)

{

;

}

elseif((cur=='\''||cur=='\"')&&!

convert&&stack.Count==0&&!

isValue)

{

sb.Length=0;

stack.Push(cur);

}

elseif((cur=='\''||cur=='\"')&&!

convert&&stack.Count>0&&stack.Peek()==cur&&!

isValue)

{

stack.Pop();

}

elseif((cur=='['||cur=='{')&&stack.Count==0)

{

stackType.Push(cur=='['?

']':

'}');

sb.Append(cur);

}

elseif((cur==']'||cur=='}')&&stack.Count==0&&stackType.Peek()==cur)

{

stackType.Pop();

sb.Append(cur);

}

elseif(cur==':

'&&stack.Count==0&&stackType.Count==0&&!

isValue)

{

last=newJsonProperty();

this[sb.ToString()]=last;

isValue=true;

sb.Length=0;

}

elseif(cur==','&&stack.Count==0&&stackType.Count==0)

{

if(last!

=null)

{

Stringtemp=sb.ToString().IndexOf("\\u")==1?

TransCode(sb.ToString()):

sb.ToString();

last.Parse(reftemp);

}

isValue=false;

sb.Length=0;

}

else

{

sb.Append(cur);

}

}

if(sb.Length>0&&last!

=null&&last.Type==JsonPropertyType.Null)

{

Stringtemp=sb.ToString().IndexOf("\\u")==1?

TransCode(sb.ToString()):

sb.ToString();

last.Parse(reftemp);

}

}

//获取指定名称的属性

publicJsonPropertythis[StringpropertyName]

{

get

{

JsonPropertyresult=null;

if(this.mProperty!

=null&&this.mProperty.ContainsKey(propertyName))

{

result=this.mProperty[propertyName];

}

returnresult;

}

set

{

if(this.mProperty==null)

{

this.mProperty=newDictionary();

}

}

}

//通过此泛型函数可直接获取指定类型属性的值

publicvirtualTProperties(StringPropertyName)whereT:

class

{

JsonPropertyp=this[PropertyName];

if(p!

=null)

{

returnp.GetValue();

}

returndefault(T);

}

//获取属性名称列表

publicString[]GetPropertyNames()

{

if(this.mProperty==null)

returnnull;

String[]keys=null;

if(this.mProperty.Count>0)

{

keys=newString[this.mProperty.Count];

this.mProperty.Keys.CopyTo(keys,0);

}

returnkeys;

}

//移除一个属性

publicJsonPropertyRemoveProperty(StringpropertyName)

{

if(this.mProperty!

=null&&this.mProperty.ContainsKey(propertyName))

{

JsonPropertyp=this.mProperty[propertyName];

this.mProperty.Remove(propertyName);

returnp;

}

returnnull;

}

//是否为空对象

publicboolIsNull()

{

returnthis.mProperty==null;

}

//转换成字符串

publicoverrideStringToString()

{

returnthis.ToString("");

}

//指定格式表达式,转换成字符串

publicvirtualstringToString(Stringformat)

{

if(this.IsNull())

{

return"{}";

}

StringBuildersb=newStringBuilder();

foreach(Stringkeyinthis.mProperty.Keys)

{

sb.Append(",");

//sb.Append(key).Append(":

");

sb.Append("\""+key+"\"").Append(":

");

sb.Append(this.mProperty[key].ToString(format)));

}

if(this.mProperty.Count>0)

{

sb.Remove(0,1);

}

sb.Insert(0,"{");

sb.Append("}");

returnsb.ToString();

}

//JSON对象属性

publicclassJsonProperty

{

privateJsonPropertyTypemType;

privateStringmValue;

privateJsonObjectmObject;

privateListmList;

privateboolmBool;

privatedoublemNumber;

}

//创建一个空的JSON对象属性

publicJsonProperty()

{

this.mType=null;

this.mValue=null;

this.mObject=null;

this.mList=null;

}

//创建一个JSON对象属性

publicJsonProperty(Objectvalue)

{

this.SetValue(value);

}

//通过字符串构造一个JSON对象属性

publicJsonProperty(StringjsonString)

{

this.Parse(refjsonString);

}

//Json字符串解析

publicvoidParse(refStringjsonString)

{

if(String.IsNullOrEmpty(jsonString))

{

this.SetValue(null);

}

else

{

stringfirst=jsonString.Substring(0,1);

stringlast=jsonString.Substring(jsonString.Length-1,1);

//集合

if(first=="["&&last=="]")

{

this.SetValue(this.ParseArray(refjsonString));

}

//JSON对象

elseif(first=="{"&&last=="}")

{

this.SetValue(this.ParseObject(refjsonString));

}

//字符串类型

elseif((first=="'"||first=="\"")&&first==last)

{

this.SetValue(this.ParseString(refjsonString));

}

//Bool类型

elseif(jsonString=="true"||jsonString=="false")

{

this.SetValue(jsonString=="true"?

true:

false);

}

//空类型

elseif(jsonString=="null")

{

this.SetValue(null);

}

else

{

doubled=0;

//数值类型

if(double.TryParse(jsonString,outd))

{

this.SetValue(d);

}

else

{

this.SetValue(jsonString);

}

}

}

}

//JsonArray解析

privateListParseArray(refStringjsonString)

{

Listlist=newList();

intlen=jsonString.Length;

StringBuildersb=newStringBuilder()

Stackstack=newStack();

StackstackType=newStack();

StackstackStrValue=newStack();//Value中的特殊字符要保留

boolconver=false;

Charcur;

for(inti=1;i<=len-2;i++)

{

cur=jsonString[i];

if(Char.IsWhiteSpace(cur)&&stack.Count==0)

{

;

}

elseif((cur=='\''&&stack.Count==0&&!

conver&&stackType.Count==0)||(cur=='\"'&&stack.Count==0&&!

conver&&stackType.Count==0))

{

sb.Length=0;

sb.Append(cur);

stack.Push(cur);

}

elseif(cur=='\\'&&stack.Count>0&&!

conver)

{

sb.Append(cur);

conver=true;

}

elseif(conver==true)

{

conver=false;

if(cur=='u')

{

sb.Append(newchar[]{cur,jsonString[i+1],jsonString[i+2],jsonString[i+3]});

i+=4;

}

else

{

sb.Append(cur);

}

}

elseif((cur=='\''||cur=='\"')&&!

conver&&stack.Count>0&&stack.Peek()==cur&&stackType.Count==0)

{

sb.Append(cur);

list.Add(newJsonProperty(sb.ToString()));

stack.Pop();

}

elseif((cur=='['||cur=='{')&&stack.Count==0)

{

if(stackType.Count==0)

{

sb.Length=0;

}

sb.Append(cur);

stackType.Push((cur=='['?

']':

'}'));

}

elseif((cur==']'||cur=='}')&&stack.Count==0&&stackType.Count>0&&stackType.Peek()==cur)

{

sb.Append(cur);

stackType.Pop();

if(stackType.Count==0)

{

list.Add(newJsonProperty(sb.ToString()));

sb.Length=0;

}

}

elseif(cur==','&&stack.Count==0&&stackType.Count==0)

{

if(sb.Length>0)

{

list.Add(newJsonProperty(sb.ToString()));

sb.Length=0;

}

}

else

{

sb.Append(cur);

}

}

if(stack.Count>0||stackType.Count>0)

{

list.Clear();

thrownewArgumentException("无法解析JsonArray对象!

");

}

elseif(sb.Length>0)

{

list.Add(newJsonProperty(sb.ToString()));

}

returnlist;

}

}

//JsonString解析

privateStringParseString(refStringjsonString)

{

intlen=jsonString.Length;

StringBuildersb=newStringBuilder();

boolconver=false;

Charcur;

for(inti=1;i<=len-2;i++)

{

cur=jsonString[i];

if(cur=='\\'&&!

conver)

{

conver=true;

}

elseif(conver==true)

{

conver=false;

if(cur=='\\'||cur=='\"'||cur=='\''||cur=='/')

{

sb.Append(cur);

}

else

{

if(cur=='u')

{

Stringtemp=newString(newchar[]{cur,jsonString[i+1],jsonString[i+2],jsonString[i+3]});

sb.Append((char)Convert.ToInt32(temp,16));

i+=4;

}

else

{

switch(cur)

{

case'b':

sb.Append('\b');

break;

case'f':

sb.Append('\f');

break;

case'n':

sb.Append('\n');

break;

case'r':

sb.Append('\r');

break;

case't':

sb.Append('\t');

break;

}

}

}

}

else

{

sb.Append(cur);

}

}

returnsb.ToString();

}

//JsonObject解析

privateJsonObjectParseObject(refStringjsonString)

{

returnnewJsonObject(jsonString);

}

//定义一个索引器,如果属性是非数组的,返回本身

publicJsonPropertythis[intindex]

{

get

{

JsonPropertyr=null;

if(this.mType==JsonPropertyType.Array)

{

if(this.mList!

=null&&(this.mList.Count-1)>=index)

{

r=this.mList[index];

}

}

elseif(index==0)

{

returnthis;

}

returnr;

}

}

//提供一个字符串索引,简化对Object属性的访问

publicJsonPropertythis[StringpropertyName]

{

get

{

if(this.mType==JsonPropertyType.Object)

{

returnthis.mObject[propertyName];

}

else

{

returnnull;

}

}

set

{

if(this.mType==JsonPropertyType.Object)

{

this.mObject[propertyName]=value;

}

else

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1