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

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

参考:https://blog.csdn.net/my98800/article/details/52953389?locationNum=1&fps=1

 #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);
            }

 前台Ajax接收数据

 //请求数据
    $.ajax({

        type: "POST",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        url: fivestarsoreUrl,
        success: function (data) {
            bigdata = data;
            var L = 0;
            for (var i in data) {
            var strHtml = '';
                strHtml += '<li>';
                strHtml += '<label >' + '<strong>' + data[i].kinds + ':</strong>' + '</label>';
                strHtml += '<label id=Lb' + data[i].id + '>' + '</label>';
                //strHtml += '<input type="text" id=txtt' + i + '>';
                strHtml += '<input type="text" id=txtt' + data[i].id + ' >';              
                strHtml += '</li>';
                L = data[i].id;
            $(listDiv).append(strHtml);
                strID += $("#LL" + L).text() + ",";
            //初始化星星
                InitStar2(5, 5, kongxin, shixin, 'Lb' + L, 'txtt' + L, 'LL' + L, bigdata);
            }
        },
        error: function () { }
    });
原文地址:https://www.cnblogs.com/yingyigongzi/p/9412993.html