参数字符串转字典数组

Posted hofmann

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了参数字符串转字典数组相关的知识,希望对你有一定的参考价值。

#region Assign parameters to specified objects

        /// <summary>
        /// Assign parameters to specified objects
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="dic">Fields/values</param>
        /// <returns></returns>
        static public T Assign<T>(Dictionary<string, string> dic) where T : new()
        
            Type myType = typeof(T);
            T entity = new T();
            var fields = myType.GetProperties();
            string val = string.Empty;
            object obj = null;

            foreach (var field in fields)
            
                if (!dic.ContainsKey(field.Name))
                    continue;
                val = dic[field.Name];

                object defaultVal;
                if (field.PropertyType.Name.Equals("String"))
                    defaultVal = "";
                else if (field.PropertyType.Name.Equals("Boolean"))
                
                    defaultVal = false;
                    val = (val.Equals("1") || val.Equals("on")).ToString();
                
                else if (field.PropertyType.Name.Equals("Decimal"))
                    defaultVal = 0M;
                else
                    defaultVal = 0;

                if (!field.PropertyType.IsGenericType)
                    obj = string.IsNullOrEmpty(val) ? defaultVal : Convert.ChangeType(val, field.PropertyType);
                else
                
                    Type genericTypeDefinition = field.PropertyType.GetGenericTypeDefinition();
                    if (genericTypeDefinition == typeof(Nullable<>))
                        obj = string.IsNullOrEmpty(val) ? defaultVal : Convert.ChangeType(val, Nullable.GetUnderlyingType(field.PropertyType));
                

                field.SetValue(entity, obj, null);
            

            return entity;
        

        #endregion

        #region Converting parameter strings to dictionary arrays 
        /// <summary>
        /// Converting parameter strings to dictionary arrays 
        /// </summary>
        static public Dictionary<String, String> GetFormData(string formData)
        
            try
            
                String[] dataArry = formData.Split(&);
                Dictionary<String, String> dataDic = new Dictionary<string, string>();
                for (int i = 0; i <= dataArry.Length - 1; i++)
                
                    String dataParm = dataArry[i];
                    int dIndex = dataParm.IndexOf("=");
                    String key = dataParm.Substring(0, dIndex);
                    String value = dataParm.Substring(dIndex + 1, dataParm.Length - dIndex - 1);
                    dataDic.Add(key, value);
                

                return dataDic;
            
            catch (Exception ex)
            
                throw new Exception(ex.Message);
            
        
        #endregion

 

以上是关于参数字符串转字典数组的主要内容,如果未能解决你的问题,请参考以下文章

数组字典转json串,不支持字符串

OC_YYModel字典转模型的几种详细用法

python 数组 变成 字典的方法

post发送JSON数据(字符串、数组、字典、自定义对象)给服务器

PHP 数组转字符串,字符串转数组

如何将数组转换成JSON-CSDN论坛