首页 > 学院 > 开发设计 > 正文

c#解析Josn(解析多个子集,数据,可解析无限级json)

2019-11-17 02:16:46
字体:
来源:转载
供稿:网友

c#解析Josn(解析多个子集,数据,可解析无限级json)

首先引用 解析类库

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace BPMS.WEB.Common{    public class CommonJsonModel : CommonJsonModelAnalyzer    {        PRivate string rawjson;        private bool isValue = false;        private bool isModel = false;        private bool isCollection = false;        public 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>        /// 当模型是对象,返回拥有的key        /// </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>        /// 当模型是对象,key对应是值,则返回key对应的值        /// </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)                    continue;                if (model.Key == key)                    return model.Value;            }            return null;        }        /// <summary>        /// 模型是对象,key对应是对象,返回key对应的对象        /// </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>        /// 模型是对象,key对应是集合,返回集合        /// </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>        /// 当模型是值对象,返回key        /// </summary>        private string Key        {            get            {                if (IsValue())                    return base._GetKey(rawjson);                return null;            }        }        /// <summary>        /// 当模型是值对象,返回value        /// </summary>        private string Value        {            get            {                if (!IsValue())                    return null;                return base._GetValue(rawjson);            }        }    }}
View Code

  解析类父类

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表