麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

C#深入解析Json格式內容

2019-11-17 02:47:11
字體:
來源:轉載
供稿:網友

C#深入解析Json格式內容

繼上一篇《淺談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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产午夜探花 | 久久精品亚洲一区 | 九九视频精品在线观看 | 一区二区三区日韩在线观看 | 久久久线视频 | 毛片免费观看日本中文 | 久久久久久久久久久国产精品 | 国产精品久久久久久久久久东京 | 欧美激情第一区 | 在线小视频国产 | 牛牛视频在线 | 91精品国产刺激国语对白 | 激情夜色 | 中文字幕网在线 | 免费a视频在线观看 | 看免费5xxaaa毛片 | 精品一区二区久久久久 | 国产一区二区三区四区波多野结衣 | 一级毛片真人免费播放视频 | 一级毛片特黄 | 久久精品中文字幕 | 国产一级大片 | 日本教室三级在线看 | 国产羞羞视频在线免费观看 | 久久91久久久久麻豆精品 | 日本精品视频一区二区三区四区 | 免费国产自久久久久三四区久久 | www.成人在线视频 | 久久精精| 中国免费黄色 | 曰批全过程120分钟免费69 | 久久久久久久久久久一区 | 91av在线影院 | 久久中文字幕在线观看 | 精品国产视频一区二区三区 | 99亚洲| 久久久精品视频网站 | 成人国产精品一区 | 日本一级黄色大片 | 久久精品国产久精国产 | 91久久国产露脸精品国产 |