Beispiel #1
0
        public static bool SafeToBool(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            bool val = false;

            if (SafeConverter.SafeToStr(obj) == "1")
            {
                val = true;
            }
            else
            {
                bool.TryParse(SafeConverter.SafeToStr(obj), out val);
            }
            return(val);
        }
Beispiel #2
0
        /// <summary>
        /// 根据字段名获取Json值
        /// 多条记录时,获取第一条记录的值
        /// </summary>
        /// <param name="json"></param>
        /// <param name="sFieldName"></param>
        /// <returns></returns>
        public static string GetValueByFieldName(string json, string sFieldName)
        {
            if ((string.IsNullOrEmpty(json)) || (string.IsNullOrEmpty(sFieldName)))
            {
                return("");
            }
            JArray ja = (JArray)JsonConvert.DeserializeObject(json);

            if ((ja == null) || (ja.Count <= 0))
            {
                return("");
            }

            JObject jo = (JObject)(ja[0]);

            if (jo == null || !jo.ContainsKey(sFieldName))
            {
                return("");
            }

            return(SafeConverter.SafeToStr(jo[sFieldName]));
        }