使用的库是:Newtonsoft.Json 为asp.net 3.5开发的 Beta4版本,获取数据库数据用的是
Microsoft EnterpriseLibrary 4.1
其中扩展了这个库的功能,使之最适合把DataTable,DataSet,DataRow转为JSON模式
另外使用了Jquery的$.getJSON来解析后台传过来的JSON格式
另参考了:http://blog.csdn.net/dujingjing1230/archive/2009/08/28/4495008.aspx 裴旭更网友的文章
http://www.west-wind.com/Weblog/default.aspx
一个老外MVP此人专门写了一个JSON的等一系列的库,叫什么WestWind,呵呵
下面是扩展的Newtonsoft.Json几个类
DataRowConverter.cs
- using System;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Linq;
-
using System.Text;
-
using Newtonsoft.Json;
-
namespace Utility
- {
-
public class DataRowConverter : JsonConverter
- {
-
-
-
-
-
-
public override void WriteJson(JsonWriter writer, object dataRow, JsonSerializer serializer)
- {
-
DataRow row = dataRow as DataRow;
-
-
-
JsonSerializer ser = new JsonSerializer();
- writer.WriteStartObject();
-
foreach (DataColumn column in row.Table.Columns)
- {
- writer.WritePropertyName(column.ColumnName);
- ser.Serialize(writer, row[column]);
- }
- writer.WriteEndObject();
- }
-
-
-
-
-
-
-
-
public override bool CanConvert(Type valueType)
- {
-
return typeof(DataRow).IsAssignableFrom(valueType);
- }
-
-
-
-
-
-
-
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
- {
-
throw new NotImplementedException();
- }
- }
- }
DataSetConverter .cs
- using System;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Linq;
-
using System.Reflection;
-
using System.Text;
-
using Newtonsoft.Json;
-
namespace Utility
- {
-
public class DataSetConverter : JsonConverter
- {
-
-
-
-
-
-
public override void WriteJson(JsonWriter writer, object dataset, JsonSerializer serializer)
- {
-
DataSet dataSet = dataset as DataSet;
-
DataTableConverter converter = new DataTableConverter();
- writer.WriteStartObject();
-
writer.WritePropertyName("Tables");
- writer.WriteStartArray();
- BindingFlags bf = BindingFlags.Public | BindingFlags.Static;
-
foreach (DataTable table in dataSet.Tables)
- {
- converter.WriteJson(writer, table, serializer);
- }
- writer.WriteEndArray();
- writer.WriteEndObject();
- }
-
-
-
-
-
-
-
-
public override bool CanConvert(Type valueType)
- {
-
return typeof(DataSet).IsAssignableFrom(valueType);
- }
-
-
-
-
-
-
-
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
- {
-
throw new NotImplementedException();
- }
- }
- }
DataTableConverter.cs
- using System;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.Linq;
-
using System.Text;
-
using Newtonsoft.Json;
-
namespace Utility
- {
-
public class DataTableConverter : JsonConverter
- {
-
-
-
-
-
-
public override void WriteJson(JsonWriter writer, object dataTable, JsonSerializer serializer)
- {
-
DataTable table = dataTable as DataTable;
-
DataRowConverter converter = new DataRowConverter();
- writer.WriteStartObject();
-
writer.WritePropertyName("Rows");
- writer.WriteStartArray();
-
foreach (DataRow row in table.Rows)
- {
- converter.WriteJson(writer, row,serializer);
- }
- writer.WriteEndArray();
- writer.WriteEndObject();
- }
-
-
-
-
-
-
-
-
public override bool CanConvert(Type valueType)
- {
-
return typeof(DataTable).IsAssignableFrom(valueType);
- }
-
-
-
-
-
-
-
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
- {
-
throw new NotImplementedException();
- }
- }
- }
Serialize.cs
- using System;
-
using System.Collections.Generic;
-
using System.Data;
-
using System.IO;
-
using System.Linq;
-
using System.Text;
-
using Newtonsoft.Json;
-
namespace Utility
- {
-
public class Serialize
- {
-
public string Serializer(object value)
- {
- Type type = value.GetType();
-
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
- json.NullValueHandling = NullValueHandling.Ignore;
- json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
- json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
- json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
-
if (type == typeof(DataRow))
-
json.Converters.Add(new DataRowConverter());
-
else if (type == typeof(DataTable))
-
json.Converters.Add(new DataTableConverter());
-
else if (type == typeof(DataSet))
-
json.Converters.Add(new DataSetConverter());
-
StringWriter sw = new StringWriter();
-
Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
- writer.Formatting = Formatting.Indented;
-
writer.QuoteChar = '"';
- json.Serialize(writer, value);
-
string output = sw.ToString();
- writer.Close();
- sw.Close();
-
return output;
- }
-
public object Deserialize(string jsonText, Type valueType)
- {
-
Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
- json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
- json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
- json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
- json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
-
StringReader sr = new StringReader(jsonText);
-
Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
-
object result = json.Deserialize(reader, valueType);
- reader.Close();
-
return result;
- }
- }
- }
调用页面前台
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UltraWebGridJSON.aspx.cs"
-
Inherits="Web.UltraWebGridJSON" %>
-
<%@ Register Assembly="Infragistics35.WebUI.UltraWebGrid.v8.3, Version=8.3.20083.1009, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb"
-
Namespace="Infragistics.WebUI.UltraWebGrid" TagPrefix="igtbl" %>
-
<!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>
-
<mce:script src="Scripts/jquery-1.2.6.min.js" mce_src="Scripts/jquery-1.2.6.min.js" type="text/javascript"></mce:script>
-
<mce:script type="text/javascript"><!--
- $(function() {
-
var grid = igtbl_getGridById("Grid");
-
$.getJSON("UltraWebGridJSON.aspx?Json=json", function(data) {
- $.each(data.Rows, function(i, n) {
- igtbl_addNew("Grid", 0);
- grid.Rows.getRow(i).getCellFromKey("title").setValue(n.title);
- grid.Rows.getRow(i).getCellFromKey("filename").setValue(n.filename);
- grid.Rows.getRow(i).getCellFromKey("sortorder").setValue(n.sortorder);
- });
- grid.Rows.getRow(0).scrollToView();
- })
- })
-
-
// --></mce:script>
-
<mce:script type="text/javascript" id="igClientScript"><!--
- function Grid_InitializeLayoutHandler(gridName) {
- //Add code to handle your event here.
- }
-
// --></mce:script>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div id="ddlDiv">
-
<igtbl:UltraWebGrid ID="Grid" runat="server" Height="200px" Width="100%">
-
<Bands>
-
<igtbl:UltraGridBand>
-
<RowTemplateStyle BackColor="Window" BorderColor="Window" BorderStyle="Ridge">
-
<BorderDetails WidthBottom="3px" WidthLeft="3px" WidthRight="3px"
-
六六分期app的软件客服如何联系?不知道吗?加qq群【895510560】即可!标题:六六分期
阅读:19266|2023-10-27
-
今天小编告诉大家如何处理win10系统火狐flash插件总是崩溃的问题,可能很多用户都不知
阅读:10011|2022-11-06
-
今天小编告诉大家如何对win10系统删除桌面回收站图标进行设置,可能很多用户都不知道
阅读:8338|2022-11-06
-
今天小编告诉大家如何对win10系统电脑设置节能降温的设置方法,想必大家都遇到过需要
阅读:8705|2022-11-06
-
我们在使用xp系统的过程中,经常需要对xp系统无线网络安装向导设置进行设置,可能很多
阅读:8652|2022-11-06
-
今天小编告诉大家如何处理win7系统玩cf老是与主机连接不稳定的问题,可能很多用户都不
阅读:9680|2022-11-06
-
电脑对日常生活的重要性小编就不多说了,可是一旦碰到win7系统设置cf烟雾头的问题,很
阅读:8639|2022-11-06
-
我们在日常使用电脑的时候,有的小伙伴们可能在打开应用的时候会遇见提示应用程序无法
阅读:8008|2022-11-06
-
今天小编告诉大家如何对win7系统打开vcf文件进行设置,可能很多用户都不知道怎么对win
阅读:8675|2022-11-06
-
今天小编告诉大家如何对win10系统s4开启USB调试模式进行设置,可能很多用户都不知道怎
阅读:7544|2022-11-06
|
请发表评论