繼上一篇《淺談C#手動解析Json格式內容》我又來分析加入了一些功能讓 這個解析類更實用
本章節最會開放我最終制作成功的Anonymous.Json.dll這個解析庫 需要的拿走~
功能繼上一篇增加了許多上一篇只是講述了 解析的步驟但是 至于一些擴展的功能卻沒有涉及
本文將繼續講解
1.如何將json轉換為一個類或者結構 甚至屬性
2.如何將一個類或者結構甚至屬性轉換為json
就這兩點就已經很頭疼了 誒 廢話不多說進入正題
上一篇一直有個很神秘的JsonObject沒有講解 現在先來揭開JsonObject的神秘面紗
internal bool _isArray = false;/// <summary>/// 是否為json array類型/// </summary>public bool IsArray{ get { return _isArray; }}internal bool _isString = false;/// <summary>/// 是否為json string類型/// </summary>public bool IsString{ get { return _isString; }}internal bool _isBool = false;/// <summary>/// 是否為json bool類型/// </summary>public bool IsBool{ get { return _isBool; }}internal bool _isObject = false;/// <summary>/// 是否為json object類型/// </summary>public bool IsObject{ get { return _isObject; }}internal bool _isChar = false;/// <summary>/// 是否為json char類型/// </summary>public bool IsChar{ get { return _isChar; }}internal bool _isInt;/// <summary>/// 是否為json 整數型/// </summary>public bool IsInt{ get { return _isInt; }}internal bool _isLong;/// <summary>/// 是否為json 長整數型/// </summary>public bool IsLong{ get { return _isLong; }}internal bool _isDouble;/// <summary>/// 是否為json 浮點型/// </summary>public bool IsDouble{ get { return _isDouble; }}internal bool _isNull = false;/// <summary>/// 是否為json null/// </summary>public bool IsNull{ get { return _isNull; }}/// <summary>/// 將object轉換為JsonObject/// </summary>/// <param name="obj"></param>public JsonObject(object obj){ ConvertToJsonObject(this, obj);}/// <summary>/// 定義一個任意類型的隱式轉換/// </summary>/// <param name="obj"></param>/// <returns></returns>public static implicit Operator JsonObject(string obj){ return ImplicitConvert(obj);}public static implicit operator JsonObject(int obj){ return ImplicitConvert(obj);}public static implicit operator JsonObject(double obj){ return ImplicitConvert(obj);}public static implicit operator JsonObject(float obj){ return ImplicitConvert(obj);}public static implicit operator JsonObject(long obj){ return ImplicitConvert(obj);}public static implicit operator JsonObject(decimal obj){ return ImplicitConvert(obj);}PRivate static JsonObject ImplicitConvert(object convert){ JsonObject obj = new JsonObject(); obj.ValueConvertToJsonObject(convert.ToString(), false); return obj;}/// <summary>/// 轉換形態/// </summary>/// <param name="parent"></param>/// <param name="sourceObj"></param>/// <returns>如果是基本類型返回false直接進行設置</returns>private bool ConvertToJsonObject(JsonObject parent, object sourceObj){ if (sourceObj == null) return false; Type t = sourceObj.GetType(); if (t.IsGenericType) { Type ctorType = t.GetGenericTypeDefinition(); if (ctorType == typeof(List<>)) { parent._isArray = true; parent._sourceObj = new List<JsonObject>(); int count = (int)t.GetProperty("Count").GetValue(sourceObj, null); MethodInfo get = t.GetMethod("get_Item"); for (int i = 0; i < count; i++) { object value = get.Invoke(sourceObj, new object[] { i }); JsonObject innerObj = new JsonObject(); if (!ConvertToJsonObject(innerObj, value)) { innerObj.ValueConvertToJsonObject(value.ToString(), false); } parent.add(innerObj); } } else if (ctorType == typeof(Dictionary<,>)) { parent._isObject = true; parent._sourceObj = new Dictionary<string, JsonObject>(); int count = (int)t.GetProperty("Count").GetValue(sourceObj, null); object kv_entity = t.GetMethod("GetEnumerator").Invoke(sourceObj, null); Type entityType = kv_entity.GetType(); for (int i = 0; i < count; i++) { bool mNext = (bool)entityType.GetMethod("MoveNext").Invoke(kv_entity, null); if (mNext) { object current = entityType.GetProperty("Current").GetValue(kv_entity, null); Type currentType = current.GetType(); object key = currentType.GetProperty("Key").GetValue(current, null); object value = currentType.GetProperty("Value").GetValue(current, null); if (!(key is string)) throw new Exception("json規范格式不正確 Dictionary起始key應為string類型"); JsonObject innerObj = new JsonObject(); innerObj._key = key.ToString(); if (!ConvertToJsonObject(innerObj, value)) { innerObj.ValueConvertToJsonObject(value.ToString(), false); } parent.add(innerObj); } } } else { throw new Exception("不支持的泛型操作"); } return true; } else if (t.IsArray) { parent._isArray = true; parent._sourceObj = new List<JsonObject>(); int rank = t.GetArrayRank(); if (rank > 1) { throw new Exception("暫不支持超過1維的數組"); } else { int length_info = Convert.ToInt32(t.GetProperty("Length").GetValue(sourceObj, null)); for (int i = 0; i < length_info; i++) { object innerObj = t.GetMethod("GetValue", new Type[] { typeof(int) }).Invoke(sourceObj, new object[] { i }); JsonObject obj = new JsonObject(); if (!ConvertToJsonObject(obj, innerObj)) { obj.ValueConvertToJsonObject(innerObj.ToString(), false); } parent.add(obj); } } return true; } else if ((t.IsValueType && !t.IsPrimitive) || (t.IsClass && !(sourceObj is string))) { parent._isObject = true; parent._sourceObj = new Dictionary<string, JsonObject>(); PropertyInfo[] infos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo item in infos) { JsonObject innerObj = new JsonObject(); innerObj._key = item.Name; object obj = item.GetValue(sourceObj, null); if (!ConvertToJsonObject(innerObj, obj)) { innerObj.ValueConvertToJsonObject(obj == null ? "null" : obj.ToString(), false); } parent.add(innerObj); } return true; } else { parent.ValueConvertToJsonObject(sourceObj.ToString(), false); return false; }}public JsonObject() { }/// <summary>/// 如果為json object提取索引內容/// </summary>/// <param name="index">key</param>/// <returns></returns>public JsonObject this[string index]{ get { if (IsObject) { if (ContainsKey(index)) { return dictionary()[index]; } else { throw new Exception("不包含 key: " + index); } } else { throw new Exception("該對象不是一個json object類型請用IsObject進行驗證后操作"); } } set { if (IsObject) { if (value is JsonObject) { dictionary()[index] = value; } else { dictionary()[index] = new JsonObject(value); } } else { throw new Except
新聞熱點
疑難解答