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

先引用
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值