转换  参考: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(, "", "add_time asc");
List<Model.fivestarsore> models = new List<Model.fivestarsore>(); if (ds == null || ds.Tables.Count <= )
return null ; DataTable dt = ds.Tables[]; for (int i = ; i < dt.Rows.Count; i++)
{
//创建泛型对象
Model.fivestarsore model2 = Activator.CreateInstance<Model.fivestarsore>();
//获取对象所有属性
PropertyInfo[] propertyInfo = model2.GetType().GetProperties();
for (int j = ; 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 () { }
});

C#将List<>转换为Json,将DataSet转成List<T>的更多相关文章

  1. Asp.net 将DataTable 或者DataSet 转换为Json 格式

    Web 开发中,将从数据库中取到的数据直接转换为 Json 格式的数据,在前台通过Ajax 无刷新显示在界面上,下面提供将DataTable 或者DataSet 转换为Json 的方法 /// < ...

  2. 将Json数据转换为ADO.NET DataSet对象

    Json数据转换为ADO.NET DataSet其实方法有很多,Newtonsoft.Json也提供了DataSet的Converter用以转换Json数据.但是有些情况下DataSet Conver ...

  3. c# DataSet转换为Json

    /// <summary> /// DataSet转换为Json /// </summary> /// <param name="dataSet"&g ...

  4. LINQ返回DataTable类型 list转dataset 转换为JSON对象

    using System.Web.Script.Serialization; using System.Collections.Generic; using System.Reflection; us ...

  5. c#常用的Datable转换为json,以及json转换为DataTable操作方法

    #region  DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  6. C#中把Datatable转换为Json的5个代码实例

    一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table" ...

  7. 将DataSet转换成json

     /// <summary>        /// 把dataset数据转换成json的格式        /// </summary>        /// <para ...

  8. js压缩xml字符串,将xml字符串转换为xml对象,将xml对象转换为json对象

    /** * 压缩xml字符串 */ function compressXmlStr(str){ var prefix, suffix; var i = str.indexOf("\r&quo ...

  9. 数据集转换为Json

    数据集转换为Json 第一步:新建一个类对象  通常我会写三个属性:状态.返回信息.数据集 第二步:新建一个JSON转换类 第三步:把类对象当做参数传入JSON转换类 ———————————————— ...

随机推荐

  1. [No0000171]wpf 类层次结构Class Hierarchy

    1.DispatcherObject类表示具有相关联的对象分派器.调度程序处理要在特定线程上执行工作的请求队列.它能够在其关联的线程上调用方法. DispatcherObject是对象的基类,用于跟踪 ...

  2. [No0000CE]检测非空格字符作为密码的密码强度

    Regex.Replace(pwd, "^(?:([a-z])|([A-Z])|([0-9])|(.)){6,}|(.)+$", "$1$2$3$4$5").L ...

  3. shell脚本之分析oracle数据库数据泵日志中表的大小

    1.分析日志格式如下 . . imported "xxx_330508"."xxx_T_DATA" 46.17 MB 268 rows . . imported ...

  4. 一个生产可用的mysql参数文件my.cnf

    [client]#客户端选项设置#设置客户端和连接字符集default_character_set = utf8port = 3306socket = /opt/mysql-5.6.24/tmp/my ...

  5. python-----多线程、线程池、进程池

    import threadingimport time ###############################多线程################################------ ...

  6. 分区实践 注意分区名 p2018-01 p2018-02 被解释为同一分区名

    # https://dev.mysql.com/doc/refman/5.6/en/partitioning-columns-range.html'''CREATE TABLE employees ( ...

  7. scrollView截取指定区域的图片

    把scrollView放到一个容器里面,再截图就可以了 scrollview放到容器: UIView *lunboCarrier = [[UIView alloc] initWithFrame:CGR ...

  8. [报错]ios开发 failed to read file attributes for

    下载第三方demo,运行报错:   failed to read file attributes for https://stackoverflow.com/questions/46301270/fa ...

  9. LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告

    题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  10. Excel--数据对比方法

    1.函数对比: 适用于两列数据对比 =IF(EXACT(A2,B2)=TRUE,"相同","不同") 2.快捷键对比: 适用于少数数据对比 选中对比两列数据,快 ...