转换  参考: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. ROS基础

    在ROS中启动Gazebo物理仿真环境 roslaunch gazebo_ros empty_world.launch 打开后一片漆黑是以为gazebo需要从国外的网站上下载模型,国内网络不行,一直下 ...

  2. slam course

    视频地址:https://www.youtube.com/watch?v=wVsfCnyt5jA 课程网站:http://ais.informatik.uni-freiburg.de/teaching ...

  3. vins-mono源码解读

    https://blog.csdn.net/q597967420/article/details/76099409

  4. Django:视图views(三)

    写一下Cookie.重定向.Session Cookie 测试代码,承接前面的代码: 路由: booktest/urls.py urlpatterns = [ url('^$',views.index ...

  5. java设计模式--单例

    GOF23(group of four)---由4个大牛总结的JAVA23种常用的设计模式,归根结底都是为了代码的可扩展性. 设计模式中一种比较重要的思想就是:开闭原则,尽量做到对扩展开放,对修改关闭 ...

  6. [development][dpdk] dpdk-pmdinfo 工具的使用

    发现了一个工具 dpdk-pmdinfo 但是文档介绍的很不清楚, http://dpdk.org/doc/guides/tools/pmdinfo.html 经过摸索, 这可能就是一个查询 dpdk ...

  7. 搭建Kubernetes服务集群遇到的问题

    kube-proxy问题: Apr 12 09:42:49 compute1 kube-proxy[12965]: E0412 09:42:49.602342 12965 reflector.go:2 ...

  8. Maven基本介绍及安装

    什么是Maven 是一个跨平台的项目管理工具. 跨平台是指它几乎可以在现有所有流行的操作系统中运行 maven不仅可以构建项目,还可以依赖管理和项目信息管理 Maven解决了什么问题 maven解决了 ...

  9. 洛谷P3567 KUR-Couriers [POI2014] 主席树/莫队

    正解:主席树/莫队 解题报告: 传送门! 这题好像就是个主席树板子题的样子,,,? 毕竟,主席树的最基本的功能就是,维护一段区间内某个数字的个数 但是毕竟是刚get到主席树,然后之前做的一直是第k大, ...

  10. 注解之@PathVariable

    @PathVariable只支持一个属性value,类型是为String,代表绑定的属性名称.默认不传递时,绑定为同名的形参. 用来便捷地提取URL中的动态参数.其英文注释如下: Annotation ...