添加下面两句:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
代码:
==================================================
DataTable dt= new DataTable();
dt.Columns.Add("a",Type.GetType("System.String"));
dt.Columns.Add("b",Type.GetType("System.String"));
dt.Columns.Add("c",Type.GetType("System.String"));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["a"] = "1" + i.ToString();
dr["b"] = "2" + i.ToString();
dr["c"] = "3" + i.ToString();
dt.Rows.Add(dr);
}
string sj = JsonConvert.SerializeObject(dt,new DataTableConverter());
//string dt1 = (string)JsonConvert.DeserializeObject(strJson);
Response.Write(sj);
输出结果:
[{"a":"10","b":"20","c":"30"},{"a":"11","b":"21","c":"31"},{"a":"12","b":"22","c":"32"},{"a":"13","b":"23","c":"33"},{"a":"14","b":"24","c":"34"},{"a":"15","b":"25","c":"35"},{"a":"16","b":"26","c":"36"},{"a":"17","b":"27","c":"37"},{"a":"18","b":"28","c":"38"},{"a":"19","b":"29","c":"39"}]
以上实现了datatable序列化!!
再通过下面这三名,可以把Dataset传给Datatabel,就可以实现数据库到json的序列化
DataSetds=得到数据集;
if(ds.Tables.Count>0)
DataTabledt=ds.Tables[0];
其实很简单吧!!
下面介绍List和 Dectionary集合的序列化和反序列化:
List序列化
List<Customer> _list = new List<Customer>();
_list.Add(new Customer { Unid=1,CustomerName="宋江",Memo="天魁星"});
_list.Add(new Customer { Unid = 1,CustomerName = "吴用",Memo = "天机星" });
_list.Add(new Customer { Unid = 1,CustomerName = "石秀",Memo = "天慧星" });
string strJson = JsonConvert.SerializeObject(_list,Formatting.Indented);
/*
[{"ID": 1,"CustomerName": "宋江","Memo": "天魁星"},
{"ID": 1,"CustomerName": "吴用","Memo": "天机星"},"CustomerName": "石秀","Memo": "天慧星"}]
*/
2)List反序列化
反序列化一个 json 对象到一个.net 集合,只需指定要反序列化的集合类型即可。
json.net 支持大量的集合类型。
string strJson = @"[{""ID"": 1,""CustomerName"": ""宋江"",""Memo"": ""天魁星
""},{""ID"": 1,""CustomerName"": "" 吴用 "",""Memo"": "" 天机星 ""},{""ID"":
1,""CustomerName"": ""石秀"",""Memo"": ""天慧星""}]";
List<Customer> _list = JsonConvert.DeserializeObject<List<Customer>>(strJson);
foreach (Customer c in _list)
{
Console.WriteLine(c.CustomerName);
}
/*
宋江
吴用
石秀
*/
3)Dictionary反序列化
json.net 也能把 json 对象反序列化为一个泛型字典。json 对象的属性名和值会被添
加到一个字典中。
string strJson = @"{""ID"": 1,""Memo"": ""天魁星""}";
Dictionary<string,string> _dictionary = JsonConvert.DeserializeObject<Dictionary<string,
string>>(strJson);
foreach (KeyValuePair<string,string> kp in _dictionary)
{
Console.WriteLine(kp.Key + ":" + kp.Value);
}
看到这里是不是有些“懵”,这不就是集合的“字符串”化吗?没什么大不了的。非也,这只是服务器端的响应,要结合起客户端的json2.js+jqery执行起来,效果,一般人我不告诉他!
=========================================================
CS代码:
DataTable dt= new DataTable();
dt.Columns.Add("a",Type.GetType("System.String"));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["a"] = "1" + i.ToString();
dr["b"] = "2" + i.ToString();
dr["c"] = "3" + i.ToString();
dt.Rows.Add(dr);
}
string sj = JsonConvert.SerializeObject(dt,new DataTableConverter());
Response.Write(sj);
Response.End();
======================================================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div style=" margin:50px;" id="f">
</div>
</form>
<script type="text/javascript">
$.getJSON("default.aspx",function (data) {
var ss = "";
for (var i = 0; i < getJsonLength(data); i++) {
ss += data[i].a + data[i].b + data[i].c + "<br>";
}
$("#f").html(ss);
});
function getJsonLength(json) {
var len = 0;
if (Boolean(json)) {
for (i in json) len++;
}
return len;
}
</script>
</body>
</html>
简单的说明了一下整个json序列化和反序列化,客户端的使用,有什么不明白的就百度+谷歌!
====================================
补充说明一下,在序列化日期型的数据时,需要加上一个
JavaScriptDateTimeConverter mTimeConverter = new JavaScriptDateTimeConverter();
或者
IsoDateTimeConverter mTimeConverter = new IsoDateTimeConverter();
的格式说明!IsoDateTimeConverter和JavaScriptDateTimeConverter类都是来至 Newtonsoft.Json,所以在使用时,记得引用该插件(using Newtonsoft.Json、using Newtonsoft.Json.Converters)
比如:
上面的dr["c"] = "3" + i.ToString();改成dr["c"] = DateTime.Now;
那么序列化的时候就要改成
string sj = JsonConvert.SerializeObject(dt,new DataTableConverter(),new IsoDateTimeConverter());
或者
string sj = JsonConvert.SerializeObject(dt,JavaScriptDateTimeConverter());
返回的值格式是不一样的,一个是时间戳,一个是具体的时候字符串。