• 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 () { }
        });
  • 相关阅读:
    Get Date information with a datetime var
    深入浅出Eclipse Modeling Framework (EMF)
    [转]How to Create a Thumbnail Picture Library View in SharePoint 2007
    Moss2007 view 过滤 当前用户 Custom List View (Filter by User [Me])
    moss 2007 自定义NewForm.aspx 注意事项 报错Unable to display this Web Part
    [转]Hiding the Search Scopes DropDown in WSSv3/MOSS 2007
    moss2007 新建模板页 搜索框无法使用 SmallSearchInputBox
    [转]SharePoint SearchasYouType with jQuery
    [转]Moss2007 Customize the NewForm.aspx 自定义NewForm EditForm页面
    Moss2007 xslt DataFormWebPart 展示图片库所有图片(文档库所有文档)
  • 原文地址:https://www.cnblogs.com/yingyigongzi/p/9412993.html
Copyright © 2020-2023  润新知