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(, "", "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>的更多相关文章
- Asp.net 将DataTable 或者DataSet 转换为Json 格式
Web 开发中,将从数据库中取到的数据直接转换为 Json 格式的数据,在前台通过Ajax 无刷新显示在界面上,下面提供将DataTable 或者DataSet 转换为Json 的方法 /// < ...
- 将Json数据转换为ADO.NET DataSet对象
Json数据转换为ADO.NET DataSet其实方法有很多,Newtonsoft.Json也提供了DataSet的Converter用以转换Json数据.但是有些情况下DataSet Conver ...
- c# DataSet转换为Json
/// <summary> /// DataSet转换为Json /// </summary> /// <param name="dataSet"&g ...
- LINQ返回DataTable类型 list转dataset 转换为JSON对象
using System.Web.Script.Serialization; using System.Collections.Generic; using System.Reflection; us ...
- c#常用的Datable转换为json,以及json转换为DataTable操作方法
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...
- C#中把Datatable转换为Json的5个代码实例
一. /// <summary> /// Datatable转换为Json /// </summary> /// <param name="table" ...
- 将DataSet转换成json
/// <summary> /// 把dataset数据转换成json的格式 /// </summary> /// <para ...
- js压缩xml字符串,将xml字符串转换为xml对象,将xml对象转换为json对象
/** * 压缩xml字符串 */ function compressXmlStr(str){ var prefix, suffix; var i = str.indexOf("\r&quo ...
- 数据集转换为Json
数据集转换为Json 第一步:新建一个类对象 通常我会写三个属性:状态.返回信息.数据集 第二步:新建一个JSON转换类 第三步:把类对象当做参数传入JSON转换类 ———————————————— ...
随机推荐
- dma传输数据长度,在启动前必须确保是一个大于0的数字
否则将导致不能接受数据
- [No0000172]Android Studio设置HTTP代理(可用)
android SDK下载:http://www.androiddevtools.cn . 禁止第一次启动 到AS安装目录,打开bin目录,编辑idea.properties, 在文件末尾添加: di ...
- Oracle 变量之 DDL_LOCK_TIMEOUT
DDL_LOCK_TIMEOUTProperty DescriptionParameter type IntegerDefault value 0Modifiable ALTER SESSIONRan ...
- python面向对象:类方法
类的方法包括以下几种: 构造方法 :__init__(self,) 析构方法 :__del__(self) 类方法@classmethod.实例方法.静态方法@staticmethod 一.构造方法 ...
- English class 81:How Vulnerability can make our lives better?
1,can we come off as weak if we show imperfections? 2,The first thing I look for in you and the last ...
- Go编写的并行计算示例程序
Go编写的并行计算示例程序 package main import "fmt" const ngoroute = 1000000 func f(left, right chan i ...
- Page10:Lyapunov稳定概念及判定定理[Linear System Theory]
内容包含连续和离散系统的Lyapunov稳定概念及其各种判别定理
- OC中如何优化代理是否响应某个方法
看以下示例代码: if([_delegate respondsToSelector: @selector(someClassDidSomething:)){ [_delegate someClassD ...
- [DPI] Cisco Application Visibility and Control
DPI,可分为三部分: https://blogs.cisco.com/enterprise/cisco-traffic-analysis-encrypted-threat-analytics 知名端 ...
- 2018/03/08 每日一个Linux命令 之 chattr/lsattr
每日一个Linux命令 2018-03-08 Linux 命令 chattr/lsattr chattr [-参数] [+/-属性] [文件或者目录] 经过今天没有对铃,粥熬糊了,我就知道...... ...