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

2021-07-15 15:14

阅读:698

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

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

 #region 将List转换为Json
        public string List2JSON(List 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 = "{";
            Liststring> ls_propertys = new Liststring>();
            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 Liststring> GetObjectProperty(object o)
        {
            Liststring> propertyslist = new Liststring>();
            PropertyInfo[] propertys = o.GetType().GetProperties();
            foreach (PropertyInfo p in propertys)
            {
                propertyslist.Add("\"" + p.Name.ToString() + "\":\"" + p.GetValue(o, null) + "\"");
            }
            return propertyslist;
        }

        #endregion

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

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

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

            DataTable dt = ds.Tables[0];

            for (int i = 0; i )
            {
                //创建泛型对象
                Model.fivestarsore model2 = Activator.CreateInstance();
                //获取对象所有属性
                PropertyInfo[] propertyInfo = model2.GetType().GetProperties();
                for (int j = 0; 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))
                                //{
                                //    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

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

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


评论


亲,登录后才可以留言!