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