ImageVerifierCode 换一换
格式:DOCX , 页数:24 ,大小:19.19KB ,
资源ID:5222639      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5222639.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JSON帮助类cs.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JSON帮助类cs.docx

1、JSON帮助类cs/用于构建属性值的回调public delegate void SetPropertiesDelegate(JsonObject property);/JsonObject属性值类型public enum JsonPropertyType String, Object, Array, Number, Bool, Null/JSON通用对象public class JsonObject private Dictionary mProperty; /创建一个JSON对象 public JsonObject() this.mProperty = null; /通过字符串构造一个JS

2、ON对象 public JsonObject(SetPropertiesDelegate callbck) if(callback != null) callback(this); /Json字符串解析 private void Parse(ref String jsonString) int len = jsonString.Length; if(String.IsNullOrEmpty(jsonString)| jsonString.Substring(0,1) != | jsonString.Substring(jsonString.Length - 1,1) != ) throw ne

3、w ArgumentException(传入文本不符合Json格式 + jsonString); Stack stack = new Stack(); Stack stackType = new Stack(); Stack stackStrValue = new Stack(); /Value中的特殊字符需要保留 StringBuilder sb = new StringBuilder(); Char cur; bool isValue = false; JsonProperty last = null; for(int i=1;i 0 & stack.Peek() = cur & !isV

4、alue) stack.Pop(); else if (cur = | cur = ) & stack.Count = 0) stackType.Push(cur = ? : ); sb.Append(cur); else if (cur = | cur = ) & stack.Count = 0 & stackType.Peek() = cur) stackType.Pop(); sb.Append(cur); else if (cur = : & stack.Count = 0 & stackType.Count = 0 & !isValue) last = new JsonPropert

5、y(); thissb.ToString() = last; isValue = true; sb.Length = 0; else if (cur = , & stack.Count = 0 & stackType.Count = 0) if (last != null) String temp = sb.ToString().IndexOf(u) = 1 ? TransCode(sb.ToString() : sb.ToString(); last.Parse(ref temp); isValue = false; sb.Length = 0; else sb.Append(cur); i

6、f (sb.Length 0 & last != null & last.Type = JsonPropertyType.Null) String temp = sb.ToString().IndexOf(u) = 1 ? TransCode(sb.ToString() : sb.ToString(); last.Parse(ref temp); /获取指定名称的属性 public JsonProperty thisString propertyName get JsonProperty result = null; if(this.mProperty ! = null & this.mPro

7、perty.ContainsKey(propertyName) result = this.mPropertypropertyName; return result; set if(this.mProperty = null) this.mProperty = new Dictionary(); /通过此泛型函数可直接获取指定类型属性的值 public virtual T Properties(String PropertyName) where T:class JsonProperty p = thisPropertyName; if(p!=null) return p.GetValue()

8、; return default(T); /获取属性名称列表 public String GetPropertyNames() if(this.mProperty = null) return null; String keys = null; if(this.mProperty.Count0) keys = new Stringthis.mProperty.Count; this.mProperty.Keys.CopyTo(keys,0); return keys; /移除一个属性 public JsonProperty RemoveProperty(String propertyName)

9、 if(this.mProperty !=null & this.mProperty.ContainsKey(propertyName) JsonProperty p = this.mPropertypropertyName; this.mProperty.Remove(propertyName); return p; return null; /是否为空对象 public bool IsNull() return this.mProperty = null; /转换成字符串 public override String ToString() return this.ToString(); /

10、指定格式表达式,转换成字符串 public virtual string ToString(String format) if(this.IsNull() return ; StringBuilder sb = new StringBuilder(); foreach(String key in this.mProperty.Keys) sb.Append(,); /sb.Append(key).Append(:); sb.Append( + key + ).Append(:); sb.Append(this.mPropertykey.ToString(format); if(this.mPr

11、operty.Count0) sb.Remove(0,1); sb.Insert(0,); sb.Append(); return sb.ToString(); /JSON对象属性 public class JsonProperty private JsonPropertyType mType; private String mValue; private JsonObject mObject; private List mList; private bool mBool; private double mNumber; /创建一个空的JSON对象属性 public JsonProperty(

12、) this.mType = null; this.mValue = null; this.mObject = null; this.mList = null; /创建一个JSON对象属性 public JsonProperty(Object value) this.SetValue(value); /通过字符串构造一个JSON对象属性 public JsonProperty(String jsonString) this.Parse(ref jsonString); /Json字符串解析 public void Parse(ref String jsonString) if(String.I

13、sNullOrEmpty(jsonString) this.SetValue(null); else string first = jsonString.Substring(0,1); string last = jsonString.Substring(jsonString.Length -1,1); /集合 if(first = & last = ) this.SetValue(this.ParseArray(ref jsonString); /JSON对象 else if(first = & last = ) this.SetValue(this.ParseObject(ref json

14、String); /字符串类型 else if(first = | first = )& first = last) this.SetValue(this.ParseString(ref jsonString); /Bool类型 else if(jsonString = true | jsonString =false) this.SetValue(jsonString = true?true:false); /空类型 else if (jsonString = null) this.SetValue(null); else double d = 0; /数值类型 if(double.TryP

15、arse(jsonString,out d) this.SetValue(d); else this.SetValue(jsonString); /Json Array解析 private List ParseArray(ref String jsonString) List list = new List(); int len = jsonString.Length; StringBuilder sb = new StringBuilder() Stack stack = new Stack(); Stack stackType = new Stack(); Stack stackStrVa

16、lue = new Stack();/Value中的特殊字符要保留 bool conver = false; Char cur; for(int i=1;i 0 & !conver) sb.Append(cur); conver = true; else if (conver = true) conver = false; if (cur = u) sb.Append(new char cur, jsonStringi + 1, jsonStringi + 2, jsonStringi + 3 ); i += 4; else sb.Append(cur); else if (cur = | c

17、ur = ) & !conver & stack.Count 0 & stack.Peek() = cur & stackType.Count = 0) sb.Append(cur); list.Add(new JsonProperty(sb.ToString(); stack.Pop(); else if (cur = | cur = ) & stack.Count = 0) if (stackType.Count = 0) sb.Length = 0; sb.Append(cur); stackType.Push(cur = ? : ); else if (cur = | cur = )

18、& stack.Count = 0 & stackType.Count 0 & stackType.Peek() = cur) sb.Append(cur); stackType.Pop(); if (stackType.Count = 0) list.Add(new JsonProperty(sb.ToString(); sb.Length = 0; else if (cur = , & stack.Count = 0 & stackType.Count = 0) if (sb.Length 0) list.Add(new JsonProperty(sb.ToString(); sb.Len

19、gth = 0; else sb.Append(cur); if (stack.Count 0 | stackType.Count 0) list.Clear(); throw new ArgumentException(无法解析Json Array对象!); else if (sb.Length 0) list.Add(new JsonProperty(sb.ToString(); return list; /Json String解析 private String ParseString(ref String jsonString) int len = jsonString.Length;

20、 StringBuilder sb = new StringBuilder(); bool conver = false; Char cur; for (int i = 1; i = index) r = this.mListindex; else if(index = 0) return this; return r; /提供一个字符串索引,简化对Object属性的访问 public JsonProperty thisString propertyName get if(this.mType = JsonPropertyType.Object) return this.mObjectpropertyName; else return null; set if(this.mType = JsonPropertyType.Object) this.mObjectpropertyName = value; else

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

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