1.首先下个Newtonsoft.Json.dll(CSDN上巨多)
2.
//DataTable转Json数据 源DataTable,自己命名个表明
public static string DataTableToJSON(DataTable dt, string dtName)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jw = new JsonWriter(sw))
{
JsonSerializer ser = new JsonSerializer();
jw.WriteStartObject();
jw.WritePropertyName(dtName);
jw.WriteStartArray();
foreach (DataRow dr in dt.Rows)
{
jw.WriteStartObject();
foreach (DataColumn dc in dt.Columns)
{
jw.WritePropertyName(dc.ColumnName);
ser.Serialize(jw, dr[dc].ToString());
}
jw.WriteEndObject();
}
jw.WriteEndArray();
jw.WriteEndObject();
sw.Close();
jw.Close();
}
return sb.ToString();
}
3.使用JQuery提示效果
<script src= "<%=WebConfig.ReserverSource%>/jquery-min-lastest.js" type="text/javascript"></script>
<script src="../jsLanguage/GetJsonStudy.js" type="text/javascript"></script>
#region 智能感应
public class WebConfig
{
public static string ReserverSource = @"../js";
}
#endregion
4.前台调用Json数据并显示
/// <reference path="../js/jquery-1.3.2-vsdoc.js" />
$(function() {
$("#btnClickGetJson").click(function() {
$.getJSON("../data/GetJsonMethod.aspx", { "resultType": "json" }, function(data, txtStatus) {
// alert(data["FirstJson"].length);
// alert(data["FirstJson"][0].CityName);
var h = "<table class='L01'>";
$(data["FirstJson"]).each(function(i, user) {//{//each就像foreach循环 i 代表的是循环次数
// alert(i);
// alert(user.CityName);
h += "<tr><td>城市名:" + user.CityName + "</td><td>城市邮编:" + user.PostCode + "</td></tr>";
});
h += "</table>";
$("#divResult").html(h);
});
});
})
【注意】:
以下是具体页面后台调用数据库数据并且转化为Json数据详细代码,需要注意的是Response.Clear(); Response.Write(Bind()); Response.End(); 清空缓存内容,否则无效!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Newtonsoft.Json;
using System.Text;
using System.IO;
namespace JQuery.Data
{
public partial class GetJsonMethod : System.Web.UI.Page
{
string resultType;//传值比较
string str = ConfigurationManager.AppSettings["ConnectionString"];//连接字串
DataSet ds;//dataset
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(HttpContext.Current.Request["resultType"]))
{
resultType = HttpContext.Current.Request["resultType"].ToLower();
}
if (resultType == "json")
{
Response.Clear();
Response.Write(Bind());
Response.End();
}
else
{
}
}
}
//绑定
public string Bind()
{
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand("select * from AjaxStudy", con);
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
ds = new DataSet();
adapt.Fill(ds);
return DataTableToJSON(ds.Tables[0], "FirstJson");
}
//DataTable转Json数据
public static string DataTableToJSON(DataTable dt, string dtName)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jw = new JsonWriter(sw))
{
JsonSerializer ser = new JsonSerializer();
jw.WriteStartObject();
jw.WritePropertyName(dtName);
jw.WriteStartArray();
foreach (DataRow dr in dt.Rows)
{
jw.WriteStartObject();
foreach (DataColumn dc in dt.Columns)
{
jw.WritePropertyName(dc.ColumnName);
ser.Serialize(jw, dr[dc].ToString());
}
jw.WriteEndObject();
}
jw.WriteEndArray();
jw.WriteEndObject();
sw.Close();
jw.Close();
}
return sb.ToString();
}
}
}