Beispiel #1
0
        public override object Deserialize(IDictionary <string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            IList list = ClassHelper.CreateListInstance(type);

            foreach (object obj in dictionary.Values)
            {
                var items = obj as IEnumerable <KeyValuePair <string, object> >;
                if (items == null || !items.Any())
                {
                    continue;
                }
                object model = ClassHelper.CreateInstance(type);
                foreach (var item in items)
                {
                    KeyValuePair <string, object> detail = (KeyValuePair <string, object>)item;
                    string       name         = detail.Key;
                    PropertyInfo propertyInfo = type.GetProperty(name);
                    if (string.IsNullOrEmpty(name) || propertyInfo == null)
                    {
                        continue;
                    }
                    //ClassHelper.SetPropertyValue(model, name, detail.Value);
                    propertyInfo.SetValue(model, SafeConverter.SafeToObject(propertyInfo.PropertyType, detail.Value), null);
                }
                list.Add(model);
            }
            return(list);
        }
Beispiel #2
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 #3
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]));
        }