码迷,mamicode.com
首页 > Web开发 > 详细

json扩展

时间:2017-12-04 13:15:40      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:one   根据   summary   next   new   ring   turn   property   count   

using Newtonsoft.Json.Linq;

namespace Utility
{
    public static class JsonExt
    {
        /// <summary>
        /// 转换为JObject对象
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static JObject ToJObject(this string str)
        {
            JObject j = new JObject();
            try
            {
                j = JObject.Parse(str);
            }
            catch
            {
            }
            return j;
        }

        /// <summary>
        /// 转换为JArray对象
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static JArray ToJArray(this string str)
        {
            JArray j = new JArray();
            try
            {
                j = JArray.Parse(str);
            }
            catch
            {
            }
            return j;
        }

        /// <summary>
        /// 根据键获取值
        /// </summary>
        /// <param name="jo"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string JValue(this JObject jo, string key)
        {
            string val = string.Empty;
            try
            {
                val = jo[key].ToString();
            }
            catch
            {
            }
            return val;
        }

        /// <summary>
        /// 检查JObject是否含有指定key值
        /// </summary>
        /// <param name="jo"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckKey(this JObject jo, string key)
        {
            if (jo != null && jo.Count > 0)
                if (jo.Property(key) != null)
                    return true;
            return false;
        }
    }
}

  

json扩展

标签:one   根据   summary   next   new   ring   turn   property   count   

原文地址:http://www.cnblogs.com/XuPengLB/p/7976284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!