Json 转 指定 C#对象,给c#对象赋值

来源:互联网 发布:云计算行业发现趋势 编辑:程序博客网 时间:2024/06/05 07:33

先引用
Newtonsoft.Json.dll

//然后using Newtonsoft.Json.Linq;using Newtonsoft.Json;

方法如下

支持主表子表一起通过post发过来,逐个解析

        /// <summary>        /// 通过POST返回对象        /// </summary>        /// <param name="obj">传入要赋值的对象</param>        /// <param name="getjson">传入json字符串(解密的即可)</param>        /// <returns></returns>        public static object GetPostObj(object obj,  string getjson)        {            try            {                if (getjson.IndexOf("[") < 0)                {                    getjson = "[" + getjson + "]";                }                //序列化成对象                JArray ja = (JArray)JsonConvert.DeserializeObject(getjson);                //ja[0]["keys"].ToString()                //产生一个有值的对象 obj.GetType().GetProperties()获取所有属性                foreach (System.Reflection.PropertyInfo p in obj.GetType().GetProperties())                {                    var property = obj.GetType().GetProperty(p.Name);//获取字段的类型                    var str = ja[0][p.Name];                    if (str != null)//如果有此属性就赋值                    {                        //通过反射将属性赋值                        if (!property.PropertyType.IsGenericType)                        {                            //非泛型                            property.SetValue(obj, string.IsNullOrEmpty(ja[0][p.Name].ToString()) ? null : Convert.ChangeType(ja[0][p.Name].ToString(), property.PropertyType), null);                        }                        else                        {                            //泛型Nullable<>                            Type genericTypeDefinition = property.PropertyType.GetGenericTypeDefinition();                            if (genericTypeDefinition == typeof(Nullable<>))                            {                                property.SetValue(obj, string.IsNullOrEmpty(ja[0][p.Name].ToString()) ? null : Convert.ChangeType(ja[0][p.Name].ToString(), Nullable.GetUnderlyingType(property.PropertyType)), null);                            }                        }                        str = null;                    }                    str = null;                }                return obj; //将赋值好的对象返回            }            catch (Exception ex)            { throw ex; }        }        /// <summary>        /// 获取POST的字符串        /// </summary>        /// <param name="context">HttpContext 对象</param>        /// <returns>返回解密字符串</returns>        public static string GetPostStr(HttpContext context)        {            System.IO.Stream s = context.Request.InputStream;//获取POST的字符串            int count = 0;            byte[] buffer = new byte[1024];            System.Text.StringBuilder builder = new System.Text.StringBuilder();            while ((count = s.Read(buffer, 0, 1024)) > 0)            {                builder.Append(System.Text.Encoding.UTF8.GetString(buffer, 0, count));            }            s.Flush();            s.Close();            s.Dispose();           string getjson = HttpUtility.UrlDecode(builder.ToString());           return getjson;        }

使用方法:

Model.WT_GPJL obj = new Model.WT_GPJL(); //创建XXX对象(数据库中主表)Model.WT_GPJLS objs = new Model.WT_GPJLS();//创建XXX对象(数据库中子表)string str = GetPostStr(context);// 传入HttpContext 获取post的字符串obj = GetPostObj(obj, str) as Mes.Model.WT_GPJL; //给主表对象赋值objs = GetPostObj(objs, str) as Mes.Model.WT_GPJLS; //给子表对象赋值
0 0
原创粉丝点击