内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何理解ASP.NET萬能JSON解析器-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“如何理解ASP.NET萬能JSON解析器”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)專注于寶雞網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供寶雞營(yíng)銷型網(wǎng)站建設(shè),寶雞網(wǎng)站制作、寶雞網(wǎng)頁設(shè)計(jì)、寶雞網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造寶雞網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供寶雞網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

概念介紹
還是先簡(jiǎn)單說說Json的一些例子吧。注意,以下概念是我自己定義的,可以參考.net里面的TYPE的模型設(shè)計(jì)
如果有爭(zhēng)議,歡迎提出來探討!

1.最簡(jiǎn)單:
{"total":0}
total就是值,值是數(shù)值,等于0

2. 復(fù)雜點(diǎn)
{"total":0,"data":{"377149574" : 1}}
total是值,data是對(duì)象,這個(gè)對(duì)象包含了"377149574"這個(gè)值,等于1

3. 最復(fù)雜
{"total":0,"data":{"377149574":[{"cid":"377149574"}]}}
total是值,data是對(duì)象,377149574是數(shù)組,這個(gè)數(shù)組包含了一些列的對(duì)象,例如{"cid":"377149574"}這個(gè)對(duì)象。

有了以上的概念,就可以設(shè)計(jì)出通用的json模型了。

萬能JSON源碼:


復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
namespace Pixysoft.Json
{
    public class CommonJsonModelAnalyzer
    {
        protected string _GetKey(string rawjson)
        {
            if (string.IsNullOrEmpty(rawjson))
                return rawjson;
            rawjson = rawjson.Trim();
            string[] jsons = rawjson.Split(new char[] { ':' });
            if (jsons.Length < 2)
                return rawjson;
            return jsons[0].Replace("\"", "").Trim();
        }
        protected string _GetValue(string rawjson)
        {
            if (string.IsNullOrEmpty(rawjson))
                return rawjson;
            rawjson = rawjson.Trim();
            string[] jsons = rawjson.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (jsons.Length < 2)
                return rawjson;
            StringBuilder builder = new StringBuilder();
            for (int i = 1; i < jsons.Length; i++)
            {
                builder.Append(jsons[i]);
                builder.Append(":");
            }
            if (builder.Length > 0)
                builder.Remove(builder.Length - 1, 1);
            string value = builder.ToString();
            if (value.StartsWith("\""))
                value = value.Substring(1);
            if (value.EndsWith("\""))
                value = value.Substring(0, value.Length - 1);
            return value;
        }
        protected List<string> _GetCollection(string rawjson)
        {
            //[{},{}]
            List<string> list = new List<string>();
            if (string.IsNullOrEmpty(rawjson))
                return list;
            rawjson = rawjson.Trim();
            StringBuilder builder = new StringBuilder();
            int nestlevel = -1;
            int mnestlevel = -1;
            for (int i = 0; i < rawjson.Length; i++)
            {
                if (i == 0)
                    continue;
                else if (i == rawjson.Length - 1)
                    continue;
                char jsonchar = rawjson[i];
                if (jsonchar == '{')
                {
                    nestlevel++;
                }
                if (jsonchar == '}')
                {
                    nestlevel--;
                }
                if (jsonchar == '[')
                {
                    mnestlevel++;
                }
                if (jsonchar == ']')
                {
                    mnestlevel--;
                }
                if (jsonchar == ',' && nestlevel == -1 && mnestlevel == -1)
                {
                    list.Add(builder.ToString());
                    builder = new StringBuilder();
                }
                else
                {
                    builder.Append(jsonchar);
                }
            }
            if (builder.Length > 0)
                list.Add(builder.ToString());
            return list;
        }
    }
}


復(fù)制代碼 代碼如下:


using System;
using System.Collections.Generic;
using System.Text;
namespace Pixysoft.Json
{
    public class CommonJsonModel : CommonJsonModelAnalyzer
    {
        private string rawjson;
        private bool isValue = false;
        private bool isModel = false;
        private bool isCollection = false;
        internal CommonJsonModel(string rawjson)
        {
            this.rawjson = rawjson;
            if (string.IsNullOrEmpty(rawjson))
                throw new Exception("missing rawjson");
            rawjson = rawjson.Trim();
            if (rawjson.StartsWith("{"))
            {
                isModel = true;
            }
            else if (rawjson.StartsWith("["))
            {
                isCollection = true;
            }
            else
            {
                isValue = true;
            }
        }
        public string Rawjson
        {
            get { return rawjson; }
        }
        public bool IsValue()
        {
            return isValue;
        }
        public bool IsValue(string key)
        {
            if (!isModel)
                return false;
            if (string.IsNullOrEmpty(key))
                return false;
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                CommonJsonModel model = new CommonJsonModel(subjson);
                if (!model.IsValue())
                    continue;
                if (model.Key == key)
                {
                    CommonJsonModel submodel = new CommonJsonModel(model.Value);
                    return submodel.IsValue();
                }
            }
            return false;
        }
        public bool IsModel()
        {
            return isModel;
        }
        public bool IsModel(string key)
        {
            if (!isModel)
                return false;
            if (string.IsNullOrEmpty(key))
                return false;
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                CommonJsonModel model = new CommonJsonModel(subjson);
                if (!model.IsValue())
                    continue;
                if (model.Key == key)
                {
                    CommonJsonModel submodel = new CommonJsonModel(model.Value);
                    return submodel.IsModel();
                }
            }
            return false;
        }
        public bool IsCollection()
        {
            return isCollection;
        }
        public bool IsCollection(string key)
        {
            if (!isModel)
                return false;
            if (string.IsNullOrEmpty(key))
                return false;
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                CommonJsonModel model = new CommonJsonModel(subjson);
                if (!model.IsValue())
                    continue;
                if (model.Key == key)
                {
                    CommonJsonModel submodel = new CommonJsonModel(model.Value);
                    return submodel.IsCollection();
                }
            }
            return false;
        }

        /// <summary>
        /// 當(dāng)模型是對(duì)象,返回?fù)碛械膋ey
        /// </summary>
        /// <returns></returns>
        public List<string> GetKeys()
        {
            if (!isModel)
                return null;
            List<string> list = new List<string>();
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                string key = new CommonJsonModel(subjson).Key;
                if (!string.IsNullOrEmpty(key))
                    list.Add(key);
            }
            return list;
        }
        /// <summary>
        /// 當(dāng)模型是對(duì)象,key對(duì)應(yīng)是值,則返回key對(duì)應(yīng)的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            if (!isModel)
                return null;
            if (string.IsNullOrEmpty(key))
                return null;
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                CommonJsonModel model = new CommonJsonModel(subjson);
                if (!model.IsValue())
                    continue;
                if (model.Key == key)
                    return model.Value;
            }
            return null;
        }
        /// <summary>
        /// 模型是對(duì)象,key對(duì)應(yīng)是對(duì)象,返回key對(duì)應(yīng)的對(duì)象
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public CommonJsonModel GetModel(string key)
        {
            if (!isModel)
                return null;
            if (string.IsNullOrEmpty(key))
                return null;
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                CommonJsonModel model = new CommonJsonModel(subjson);
                if (!model.IsValue())
                    continue;
                if (model.Key == key)
                {
                    CommonJsonModel submodel = new CommonJsonModel(model.Value);
                    if (!submodel.IsModel())
                        return null;
                    else
                        return submodel;
                }
            }
            return null;
        }
        /// <summary>
        /// 模型是對(duì)象,key對(duì)應(yīng)是集合,返回集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public CommonJsonModel GetCollection(string key)
        {
            if (!isModel)
                return null;
            if (string.IsNullOrEmpty(key))
                return null;
            foreach (string subjson in base._GetCollection(this.rawjson))
            {
                CommonJsonModel model = new CommonJsonModel(subjson);
                if (!model.IsValue())
                    continue;
                if (model.Key == key)
                {
                    CommonJsonModel submodel = new CommonJsonModel(model.Value);
                    if (!submodel.IsCollection())
                        return null;
                    else
                        return submodel;
                }
            }
            return null;
        }
        /// <summary>
        /// 模型是集合,返回自身
        /// </summary>
        /// <returns></returns>
        public List<CommonJsonModel> GetCollection()
        {
            List<CommonJsonModel> list = new List<CommonJsonModel>();
            if (IsValue())
                return list;
            foreach (string subjson in base._GetCollection(rawjson))
            {
                list.Add(new CommonJsonModel(subjson));
            }
            return list;
        }

        /// <summary>
        /// 當(dāng)模型是值對(duì)象,返回key
        /// </summary>
        private string Key
        {
            get
            {
                if (IsValue())
                    return base._GetKey(rawjson);
                return null;
            }
        }
        /// <summary>
        /// 當(dāng)模型是值對(duì)象,返回value
        /// </summary>
        private string Value
        {
            get
            {
                if (!IsValue())
                    return null;
                return base._GetValue(rawjson);
            }
        }
    }
}

使用方法

public CommonJsonModel DeSerialize(string json)
{
 return new CommonJsonModel(json);
}

超級(jí)簡(jiǎn)單,只要new一個(gè)通用對(duì)象,把json字符串放進(jìn)去就行了。

針對(duì)上文的3個(gè)例子,我給出3種使用方法:

{"total":0}
CommonJsonModel model = DeSerialize(json);
model.GetValue("total") // return 0
{"total":0,"data":{"377149574" : 1}}
CommonJsonModel model = DeSerialize(json);
model.GetModel("data").GetValue("377149574") //return 1
{"total":0,"data":{"377149574":[{"cid":"377149574"}]}}
CommonJsonModel model = DeSerialize(json);
model.GetCollection("377149574").GetCollection()[0].GetValue("cid") //return 377149574

這個(gè)有點(diǎn)點(diǎn)復(fù)雜,

1. 首先377149574代表了一個(gè)集合,所以要用model.GetCollection("377149574")把這個(gè)集合取出來。

2. 其次這個(gè)集合里面包含了很多對(duì)象,因此用GetColllection()把這些對(duì)象取出來

3. 在這些對(duì)象List里面取第一個(gè)[0],表示取了":{"cid":"377149574"}這個(gè)對(duì)象,然后再用GetValue("cid")把對(duì)象的值取出來。

“如何理解ASP.NET萬能JSON解析器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

新聞名稱:如何理解ASP.NET萬能JSON解析器-創(chuàng)新互聯(lián)
文章路徑:http://m.rwnh.cn/article44/dscoee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站改版、定制網(wǎng)站、服務(wù)器托管、網(wǎng)站排名、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)
阿城市| 郎溪县| 象州县| 宕昌县| 禹州市| 赞皇县| 洞口县| 信阳市| 曲沃县| 南川市| 伊宁市| 柳林县| 南康市| 晋宁县| 台南县| 田林县| 柏乡县| 平南县| 商丘市| 略阳县| 通江县| 札达县| 大新县| 晋城| 天等县| 玉门市| 安庆市| 镇远县| 隆昌县| 临泉县| 安顺市| 颍上县| 嘉定区| 闵行区| 客服| 察雅县| 社旗县| 屯留县| 无锡市| 河曲县| 平度市|