using System; using System.Collections.Generic; using System.Reflection; using System.Text; namespace Json { public class JsonObject { private Dictionary _property; public JsonObject() { this._property = (Dictionary)null; } public JsonObject(SetProperties callback) { if (callback == null) return; callback(this); } public JsonObject(string jsonString) { this.Parse(ref jsonString); } public JsonProperty AddProperty(string PropertyName) { if (this._property == null) this._property = new Dictionary((IEqualityComparer)StringComparer.OrdinalIgnoreCase); if (!this._property.ContainsKey(PropertyName)) this._property.Add(PropertyName, new JsonProperty()); return this._property[PropertyName]; } public JsonProperty AddProperty(string PropertyName, object Value) { if (this._property == null) this._property = new Dictionary((IEqualityComparer)StringComparer.OrdinalIgnoreCase); if (!this._property.ContainsKey(PropertyName)) this._property.Add(PropertyName, new JsonProperty(Value)); return this._property[PropertyName]; } public JsonProperty AddProperty(string PropertyName, params object[] Values) { if (this._property == null) this._property = new Dictionary((IEqualityComparer)StringComparer.OrdinalIgnoreCase); if (!this._property.ContainsKey(PropertyName)) { JsonProperty jsonProperty = new JsonProperty(); foreach (object obj in Values) jsonProperty.Add(obj); this._property.Add(PropertyName, jsonProperty); } return this._property[PropertyName]; } public JsonProperty Replace(string PropertyName, object Value) { if (this._property == null) this._property = new Dictionary((IEqualityComparer)StringComparer.OrdinalIgnoreCase); if (this._property.ContainsKey(PropertyName)) this._property[PropertyName] = new JsonProperty(Value); return this._property[PropertyName]; } public T GetEntity() where T : new() { T obj = new T(); foreach (string propertyName in this.GetPropertyNames()) { PropertyInfo property = obj.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (property != null) { string s = this[propertyName].Value; Type propertyType = property.PropertyType; if (string.IsNullOrEmpty(s)) { if (propertyType.Equals((object)TypeCode.String)) property.SetValue((object)obj, (object)"", (object[])null); } else if (propertyType.Equals((object)TypeCode.Boolean)) property.SetValue((object)obj, (object)(s == "1"), (object[])null); else if (propertyType.Equals((object)TypeCode.DateTime)) { DateTime result; if (!DateTime.TryParse(s, out result)) result = DateTime.MinValue; property.SetValue((object)obj, (object)result, (object[])null); } else property.SetValue((object)obj, Convert.ChangeType((object)s, propertyType), (object[])null); } } return obj; } public static T GetEntityFromJson(JsonObject js, ref T a) { if ((object)a != null && js != null) { foreach (PropertyInfo property in a.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (js.HasProperty(property.Name) || js.HasProperty(property.Name.ToLower())) { string s; if (js[property.Name] == null) { if (js[property.Name.ToLower()] != null) s = js[property.Name.ToLower()].Value; else goto label_18; } else s = js[property.Name].Value; TypeCode typeCode = Type.GetTypeCode(property.PropertyType); try { if ((s == null || s.Length == 0) && typeCode == TypeCode.String) property.SetValue((object)a, (object)"", (object[])null); switch (typeCode) { case TypeCode.Boolean: property.SetValue((object)a, (object)(s == "1"), (object[])null); break; case TypeCode.DateTime: DateTime result; if (!DateTime.TryParse(s, out result)) result = DateTime.MinValue; property.SetValue((object)a, (object)result, (object[])null); break; default: property.SetValue((object)a, Convert.ChangeType((object)s, property.PropertyType), (object[])null); break; } } catch (Exception ex) { throw new ArgumentException("[" + property.Name + "]Json绑定失败:" + ex.Message); } label_18:; } } } return a; } public string[] GetPropertyNames() { if (this._property == null) return (string[])null; string[] array = (string[])null; if (this._property.Count > 0) { array = new string[this._property.Count]; this._property.Keys.CopyTo(array, 0); } return array; } public bool HasProperty(string PropertyName) { return this._property != null && this._property.ContainsKey(PropertyName); } public bool IsNull() { return this._property == null; } private void Parse(ref string jsonString) { int length = jsonString.Length; if (string.IsNullOrEmpty(jsonString) || jsonString.Substring(0, 1) != "{" || jsonString.Substring(jsonString.Length - 1, 1) != "}") throw new ArgumentException("传入文本不符合Json格式!" + jsonString); Stack charStack1 = new Stack(); Stack charStack2 = new Stack(); StringBuilder stringBuilder = new StringBuilder(); bool flag1 = false; bool flag2 = false; JsonProperty jsonProperty = (JsonProperty)null; for (int index = 1; index <= length - 2; ++index) { char ch = jsonString[index]; if (ch != '}') ; if (ch == ' ' && charStack1.Count == 0) stringBuilder.Append(ch); else if ((ch == '\'' || ch == '"') && (!flag1 && charStack1.Count == 0) && !flag2) { stringBuilder.Length = 0; charStack1.Push(ch); } else if ((ch == '\'' || ch == '"') && (!flag1 && charStack1.Count > 0) && (int)charStack1.Peek() == (int)ch && !flag2) { int num1 = (int)charStack1.Pop(); } else if ((ch == '[' || ch == '{') && charStack1.Count == 0) { charStack2.Push(ch == '[' ? ']' : '}'); stringBuilder.Append(ch); } else if ((ch == ']' || ch == '}') && charStack1.Count == 0 && (int)charStack2.Peek() == (int)ch) { int num2 = (int)charStack2.Pop(); stringBuilder.Append(ch); } else if (ch == ':' && charStack1.Count == 0 && charStack2.Count == 0 && !flag2) { jsonProperty = new JsonProperty(); this[stringBuilder.ToString()] = jsonProperty; flag2 = true; stringBuilder.Length = 0; } else if (ch == ',' && charStack1.Count == 0 && charStack2.Count == 0) { if (jsonProperty != null) { string jsonString1 = stringBuilder.ToString(); jsonProperty.Parse(ref jsonString1); } flag2 = false; stringBuilder.Length = 0; } else stringBuilder.Append(ch); } if (stringBuilder.Length <= 0 || jsonProperty == null || jsonProperty.Type != JsonPropertyType.Null) return; string jsonString2 = stringBuilder.ToString(); jsonProperty.Parse(ref jsonString2); } public virtual T Properties(string PropertyName) where T : class { JsonProperty jsonProperty = this[PropertyName]; if (jsonProperty != null) return jsonProperty.GetValue(); return default(T); } public JsonProperty RemoveProperty(string PropertyName) { if (this._property == null || !this._property.ContainsKey(PropertyName)) return (JsonProperty)null; JsonProperty jsonProperty = this._property[PropertyName]; this._property.Remove(PropertyName); return jsonProperty; } public override string ToString() { return this.ToString(""); } public virtual string ToString(string format) { if (this.IsNull()) return "{}"; StringBuilder stringBuilder = new StringBuilder(); foreach (string key in this._property.Keys) { stringBuilder.Append(","); stringBuilder.Append(string.Format("\"{0}\"", (object)key)).Append(": "); stringBuilder.Append(this._property[key].ToString(format)); } if (this._property.Count > 0) stringBuilder.Remove(0, 1); stringBuilder.Insert(0, "{"); stringBuilder.Append("}"); return stringBuilder.ToString(); } public JsonProperty this[string PropertyName] { get { if (this._property != null && this._property.ContainsKey(PropertyName)) return this._property[PropertyName]; return this.AddProperty(PropertyName); } set { if (this._property == null) this._property = new Dictionary((IEqualityComparer)StringComparer.OrdinalIgnoreCase); if (this._property.ContainsKey(PropertyName)) this._property[PropertyName] = value; else this._property.Add(PropertyName, value); } } } }