转换  参考: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. 在eclipse中导入hadoop jar包,和必要时导入源码包。

    1. 解药hadoop包 1, C:\hadoop-2.7.2\share\hadoop  提取出所有的 jar 包, 到 _lib 文件夹下 2,将有含有source 名称的jar包 剪切出来 3, ...

  2. AngularJS 常用的功能

    第一 迭代输出之ng-repeat标签ng-repeat让table ul ol等标签和js里的数组完美结合 例: <ul><li ng-repeat="person in ...

  3. Web开发——CSS基础

    参考: 参考:http://css.doyoe.com/ 参考:http://www.w3school.com.cn/cssref/index.asp 参考:https://www.w3cschool ...

  4. tp5, laravel, yii2我该选择哪个

    为什么写这篇文章 我个人有一个技术群,里面学什么框架的都有,经常会有人问 某某功能 在 哪个框架怎么实现,用什么框架实现更好,说道这里我大家讲一个同类型问题的笑话 某女:你能让这个论坛的人都吵起来,我 ...

  5. LeetCode 589 N-ary Tree Preorder Traversal 解题报告

    题目要求 Given an n-ary tree, return the preorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返回 ...

  6. webpack打包配置模板

    /** * Created by zzq on 2017/3/26. *///__dirname是node.js中的一个全局变量,它指向当前执行脚本所在的目录module.exports = {//注 ...

  7. 洛谷P4358密钥破解 [CQOI2016] 数论

    正解:数论 解题报告: 先,放个传送门QwQ 这题难点可能在理解题意,,, 所以我先放个题意QAQ 大概就是说,给定一个整数N,可以被拆成两个质数的成绩p*q,然后给出了一个数e,求d满足e*d=1( ...

  8. 【PyQt5-Qt Designer】QSlider滑块

    QSlider滑块 QSlider简介 QSlider小部件提供了一个垂直或水平滑块. 滑块是控制有界值的经典控件.它允许用户沿水平或垂直凹槽移动滑块手柄,并将手柄的位置转换为合法范围内的整数值. Q ...

  9. 基于sendmail的简单zabbix邮件报警

    1.修改zabbix server hostname声明:在配置zabbix的Email报警之前,需要将sendmail使用的域名进行相应的修改,系统默认为localhost.localdomain, ...

  10. oracle中is和as的区别

    在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS不能用IS:在游标(CURSOR)中只能用IS不能用AS.