转换  参考: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. [No0000120]Python教程3/9-第一个Python程序

    现在,了解了如何启动和退出Python的交互式环境,我们就可以正式开始编写Python代码了. 在写代码之前,请千万不要用“复制”-“粘贴”把代码从页面粘贴到你自己的电脑上.写程序也讲究一个感觉,你需 ...

  2. tensorflow的ckpt文件总结

    1.TensorFlow的模型文件 --checkpoint_dir | |--checkpoint | |--MyModel.meta | |--MyModel.data-00000-of-0000 ...

  3. ModelDriven & Preparable 接口

    一. Preparable接口 1.有prepare()方法,实现该接口的action在执行时,首先执行该方法,用于进行一些预处理.2.创建prepareXXX方法,则在执行XXX方法前,将先执行pr ...

  4. flash cs4 如何新增自定义控件

    1. 新增控件脚本* import gfx.controls.CoreList; import gfx.core.UIComponent; import gfx.controls.CheckBox; ...

  5. Excel使用

    筛选 1.数据->取消\使用筛选; 边框 函数 1.使用函数的话需要设置单元格格式为常规;

  6. LeetCode 728 Self Dividing Numbers 解题报告

    题目要求 A self-dividing number is a number that is divisible by every digit it contains. For example, 1 ...

  7. 2018/09/13《涂抹MySQL》【MySQL复制特性】学习笔记(六)

    推荐一首歌 - <可不可以>张紫豪 好吧,随便从排行榜上找了一首 读 第十一章<MySQL的复制特性> 总结 1:复制(Replication) 应用场景? - 提高性能 (通 ...

  8. 内存的一些magic number和debug crt(0xCCCCCCCC和0xCDCDCDCD,debug版本的CRT为了方便调试程序的初始值)

    调试过debug版本的vc程序的人一定对0xCCCCCCCC和0xCDCDCDCD这样的内存很有印象.这是debug版本的CRT为了方便调试程序,在分配出来还没有初始化的时候提供的初始值. 实际上,W ...

  9. 占满屏幕的宽高,当把textarea换成其他标签的时候,怎么才能编辑?

    $('.nav').width($(window).width()); $('.nav').height($(window).height());   当把textarea换成其他标签的时候,怎么才能 ...

  10. Java设计模式之模板模式及使用场景

    模板模式,顾名思义,就是通过模板拓印的方式. 定义模板,就是定义框架.结构.原型.定义一个我们共同遵守的约定. 定义了模板,我们的剩余工作就是对其进行充实.丰润,完善它的不足之处. 定义模板采用抽象类 ...