json格式包装

public class JsonObject
    {
        //变量

        protected System.Collections.Generic.Dictionary<String, Object> attributes;

        public System.Collections.Generic.Dictionary<String, Object> Attributes
        {
            get { return attributes; }
            set { attributes = value; }
        }

        /// <summary>
        /// JsonObject 构造函数
        /// </summary>
        public JsonObject()
        {
            attributes = new Dictionary<String, Object>();
        }
        public JsonObject(string text)
        {
            attributes = new Dictionary<String, Object>();
            if (!string.IsNullOrEmpty(text))
            {
                //parseJson
                //解析
                //object x = attributes.Keys;

            }
        }
        public int Count
        {
            get { return attributes.Count; }
        }
        public Dictionary<String, Object>.KeyCollection Keys
        {
            get { return attributes.Keys; }
        }
        public Dictionary<String, Object>.ValueCollection Values
        {
            get { return attributes.Values; }
        }
        /// <summary>
        /// 包装
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ContainsKey(string key)
        {
            return this.attributes.ContainsKey(key);
        }
        /// <summary>
        /// 设置字符属性值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void setAttribute(string key, string value)
        {
            if (attributes.ContainsKey(key))
            {
                attributes[key] = value;
            }
            else
            {
                attributes.Add(key, value);
            }
        }
        /// <summary>
        /// 设置bool属性值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void setAttribute(string key, bool value)
        {
            if (attributes.ContainsKey(key))
            {
                attributes[key] = value;
            }
            else
            {
                attributes.Add(key, value);
            }
        }
        / <summary>
        / 设置数值属性值
        / </summary>
        / <param name="key"></param>
        / <param name="value"></param>
        //public void setAttribute(string key, decimal value)
        //{
        //    if (attributes.ContainsKey(key))
        //    {
        //        attributes[key] = value;
        //    }
        //    else
        //    {
        //        attributes.Add(key, value);
        //    }
        //}

        /// <summary>
        /// 设置数值double属性值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void setAttribute(string key, double value)
        {
            if (attributes.ContainsKey(key))
            {
                attributes[key] = value;
            }
            else
            {
                attributes.Add(key, value);
            }
        }

        /// <summary>
        /// 设置object属性值
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="value">属性值</param>
        public void setAttribute(string key, JsonObject value)
        {
            if (attributes.ContainsKey(key))
            {
                attributes[key] = value;
            }
            else
            {
                attributes.Add(key, value);
            }
        }
        /// <summary>
        /// 设置array属性值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="array"></param>
        public void setAttribute(string key, JsonObject[] array)
        {
            if (attributes.ContainsKey(key))
            {
                attributes[key] = array;
            }
            else
            {
                attributes.Add(key, array);
            }
        }
        /// <summary>
        /// 设置array属性值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="array"></param>
        public void setAttribute(string key, DateTime time)
        {
            if (attributes.ContainsKey(key))
            {
                attributes[key] = time;
            }
            else
            {
                attributes.Add(key, time);
            }
        }
        /// <summary>
        /// setAttribute
        /// </summary>
        /// <param name="key"></param>
        /// <param name="array"></param>
        public void setAttribute(string key, object value)
        {
            switch ((value.GetType()).ToString())
            {
                case "System.Boolean":
                case "System.Double":
                case "Nature.BLL.Json.JsonObject":
                case "Nature.BLL.Json.JsonArray":
                case "Nature.BLL.Json.JsonObject[]":
                case "System.String":
                case "System.Datetime":
                    attributes.Add(key, value);
                    break;
                default:
                    throw new FormatException("JsonObject:setAttribute  illegal type of Attribute! " + (value.GetType()).ToString());
                //break;
            }
        }

        /// <summary>
        /// 返回属性值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>返回属性值</returns>
        public object getAttribute(string key)
        {
            return attributes[key];
        }
        public void removeAttribute(string key)
        {
            attributes.Remove(key);
        }

        /*
        public void renameAttribute(string oldKey, string newKey) {
            setAttribute(newKey, getAttribute(oldKey));
            removeAttribute(oldKey);
        }
         */
        /// <summary>
        /// 输出字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (attributes.Count == 0)
            {
                return "null";
            }
            //string str;
            sb.Append("{");
            int i = 0;
            foreach (Object key in attributes.Keys)
            {
                if (i > 0)
                {
                    sb.Append(",");
                }
                sb.Append(JsonUtil.attributeToString(key.ToString(), attributes[key.ToString()]));
                i++;
            }
            sb.Append("}  ");
            return sb.ToString();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值