国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C#深入解析Json格式內(nèi)容

2019-11-17 02:47:11
字體:
供稿:網(wǎng)友

C#深入解析Json格式內(nèi)容

繼上一篇《淺談C#手動解析Json格式內(nèi)容》我又來分析加入了一些功能讓 這個解析類更實用

本章節(jié)最會開放我最終制作成功的Anonymous.Json.dll這個解析庫 需要的拿走~

功能繼上一篇增加了許多上一篇只是講述了 解析的步驟但是 至于一些擴展的功能卻沒有涉及

本文將繼續(xù)講解

1.如何將json轉(zhuǎn)換為一個類或者結(jié)構 甚至屬性

2.如何將一個類或者結(jié)構甚至屬性轉(zhuǎn)換為json

就這兩點就已經(jīng)很頭疼了 誒 廢話不多說進入正題

上一篇一直有個很神秘的JsonObject沒有講解 現(xiàn)在先來揭開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 整數(shù)型/// </summary>public bool IsInt{    get { return _isInt; }}internal bool _isLong;/// <summary>/// 是否為json 長整數(shù)型/// </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轉(zhuǎn)換為JsonObject/// </summary>/// <param name="obj"></param>public JsonObject(object obj){    ConvertToJsonObject(this, obj);}/// <summary>/// 定義一個任意類型的隱式轉(zhuǎn)換/// </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>/// 轉(zhuǎn)換形態(tài)/// </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規(guī)范格式不正確 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維的數(shù)組");        }        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提取索引內(nèi)容/// </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
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 罗甸县| 浦北县| 美姑县| 泌阳县| 淮安市| 青冈县| 大冶市| 壶关县| 昌黎县| 赣榆县| 衡水市| 太和县| 江门市| 泾川县| 雷山县| 开江县| 文山县| 桦南县| 大荔县| 湘潭县| 伊金霍洛旗| 沾化县| 娄烦县| 新兴县| 开阳县| 和林格尔县| 赤水市| 屯门区| 奉化市| 涿鹿县| 佛山市| 承德市| 府谷县| 临朐县| 湖口县| 锡林浩特市| 宁安市| 湘乡市| 河南省| 新安县| 连云港市|