码迷,mamicode.com
首页 > Windows程序 > 详细

C#将List<>转换为Json,将DataSet转成List<T>

时间:2018-08-03 14:43:59      阅读:640      评论:0      收藏:0      [点我收藏+]

标签:相同   tty   eof   dbn   stars   null   属性   http   .net   

转换  参考:https://blog.csdn.net/u011176794/article/details/52670339

 #region 将List<>转换为Json
        public string List2JSON(List<Model.comment_fivestarsore> objlist)
        {
            string result = "";

            result += "[";
            bool firstline = true;//处理第一行前面不加","号
            foreach (object oo in objlist)
            {
                if (!firstline)
                {
                    result = result + "," + OneObjectToJSON(oo);
                }
                else
                {
                    result = result + OneObjectToJSON(oo) + "";
                    firstline = false;
                }
            }
            return result + "]";
        }

        private string OneObjectToJSON(object o)
        {
            string result = "{";
            List<string> ls_propertys = new List<string>();
            ls_propertys = GetObjectProperty(o);
            foreach (string str_property in ls_propertys)
            {
                if (result.Equals("{"))
                {
                    result = result + str_property;
                }
                else
                {
                    result = result + "," + str_property + "";
                }
            }
            return result + "}";
        }

        private List<string> GetObjectProperty(object o)
        {
            List<string> propertyslist = new List<string>();
            PropertyInfo[] propertys = o.GetType().GetProperties();
            foreach (PropertyInfo p in propertys)
            {
                propertyslist.Add("\"" + p.Name.ToString() + "\":\"" + p.GetValue(o, null) + "\"");
            }
            return propertyslist;
        }

        #endregion

将DataSet中数据表的内容转成list<T>集合

DataSet ds = bll.GetList(0, "", "add_time asc");
List<Model.fivestarsore> models = new List<Model.fivestarsore>();

 if (ds == null || ds.Tables.Count <= 0 )
                return null ;

            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //创建泛型对象
                Model.fivestarsore model2 = Activator.CreateInstance<Model.fivestarsore>();
                //获取对象所有属性
                PropertyInfo[] propertyInfo = model2.GetType().GetProperties();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    foreach (PropertyInfo info in propertyInfo)
                    {
                        //属性名称和列名相同时赋值
                        if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
                        {
                            if (dt.Rows[i][j] != DBNull.Value)
                            {
                                //if (info.PropertyType == typeof(System.Nullable<System.DateTime>))
                                //{
                                //    info.SetValue(_t, Convert.ToDateTime(dt.Rows[i][j].ToString()), null);
                                //}
                                //else
                                //{
                                info.SetValue(model2, Convert.ChangeType(dt.Rows[i][j], info.PropertyType), null);
                                //}
                                //info.SetValue(_t, dt.Rows[i][j], null);
                            }
                            else
                            {
                                info.SetValue(model2, null, null);
                            }
                            break;
                        }
                    }
                }
                models.Add(model2);
            }

 

C#将List<>转换为Json,将DataSet转成List<T>

标签:相同   tty   eof   dbn   stars   null   属性   http   .net   

原文地址:https://www.cnblogs.com/yingyigongzi/p/9412993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!