Newtonsoft.Json.dll 下载地址
http://docs.google.com/leaf?id=0B9T0APtVi1fyMDAxNGJjOTEtNjczMy00OWNlLWIwNmUtY2JiOTViOTg0MGE1&hl=zh_CN
json.js 下载地址 http://www.json.org/
Json.NET 资源网址: http://json.codeplex.com/
客户端:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="JsonClient._Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JsonAjax Demo</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="Scripts/json.js"></script>
<script type="text/javascript">
var commandLocation = "GetProductsByCategoryIDHandler.ashx";
function getProductsByStandard() {
$.ajax({
url: commandLocation,
dataType: "json",
data: "categoryID=" + 1,
success: function (json) {
show.innerHTML = json.AllProducts;
}
})
}
</script>
</head>
<body>
<input type="button" id="OK" value="OK" οnclick="getProductsByStandard()"/>
<span id="show"></span>
</body>
</html>
服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;
namespace JsonClient
{
/// <summary>
/// Summary description for GetProductsByCategoryIDHandler
/// </summary>
public class GetProductsByCategoryIDHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
int categoryID = Int32.Parse(request["categoryID"]); //传递过来的类别ID
string[] productNames = {"产品1", "产品2", "产品3", "产品4"};
//实例化JObject对象,同时为其加入一个名为AllProducts的属性
JObject json = new JObject(new JProperty("AllProducts", new JArray(productNames)));
response.ContentType = "text/plain";
response.Write(json.ToString());
}
public bool IsReusable
{
get
{
return false;
}
}
}
}