ASP輸出JSON數據及客戶端jQuery處理方法


首先ASP處理JSON需要json官方提供的JSON For ASP 封裝類文件,下載地址:http://code.google.com/p/aspjson/downloads/list

下載最新的JSON_2.0.4.asp文件備用。

1.ASP簡單JSON對像及數組輸出

Demo1.asp

<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”65001″%>
<% Response.Charset = “UTF-8″ %>
<% Response.ContentType = “text/JSON” %>
<!–#include file=”../Inc/JSON_2.0.4.asp”–>
<%
Dim Json,Array(4)
Set Json = jsObject()
Json(“Title”) = ” ASP輸出JSON數據及客戶端jQuery 處理方法”
Json(“Url”) = “#”
Json(“PubDate”) = Now()
Array(0) = “QQ空間”
Array(1)= “騰訊微博”
Array(2)= “新浪微博”
Array(3)= “網易微博”
Array(4)= “搜狐微博”
Set Json(“Share”) = jsArray() ‘數組對像
Json(“Share”) = Array ‘ASP數組直接傳給JSON數組對像
Json.Flush
Set Json = Nothing
%>

前台請求頁面Demo1.html

<!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>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>ASP輸出JSON數據及客戶端jQuery 處理方法</title>
<script type=”text/javascript” src=”../Scripts/jquery-1.7.2.min.js”></script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“#btn”).click(function(){
$.getJSON(“Demo1.asp”,function(data){
$(“#View”).html(“標題:”+data.Title+”<br />URL:”+data.Url+”<br />時間:”+data.PubDate+”<br />分享:”);
$.each(data.Share,function(i,n){//遍歷數組
$(“#View”).append(n + ” | “);
});
});
});
});
</script>
</head>

<body>
<input type=”button” id=”btn” value=”顯示” />
<div id=”View”></div>
</body>
</html>

是不是很簡單呀?

2.ASP數據庫數據輸出JSON

Demo2.asp

<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”65001″%>
<% Response.ContentType = “application/json; charset=utf-8″ %>
<!–#include file=”../Inc/JSON_2.0.4.asp”–>
<!–#include file=”../Inc/Conn.asp”–>
<%
Function QueryToJSON(dbc, sql) ‘此函數來自JSON官方JSON_UTIL_0.1.1.asp
Dim rs, jsa
Set rs = dbc.Execute(sql)
Set jsa = jsArray()
While Not (rs.EOF Or rs.BOF)
Set jsa(Null) = jsObject()
For Each col In rs.Fields
jsa(Null)(col.Name) = col.Value
Next
rs.MoveNext
Wend
Set QueryToJSON = jsa
End Function

SQLstr = “SELECT U_ID,U_Name,U_Age,U_Sex FROM User LIMIT 0,10″  ’這里使用的是MySQL數據庫

Response.Write QueryToJSON(Conn, SQLstr).Flush
%>

前台頁面Demo2.html

<!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>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>ASP輸出JSON數據及客戶端jQuery 處理方法</title>
<script type=”text/javascript” src=”../Scripts/jquery-1.7.2.min.js”></script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“#btn”).click(function(){
$.getJSON(“Demo2.asp”,function(data){
$.each(data,function(i,n){
var $tr = $(“#View”).find(“tr.Demo”).clone();
$tr.removeClass(“Demo”);
$tr.find(“td:eq(0)”).text(n.U_ID);//第一個td
$tr.find(“td:eq(1)”).text(n.U_Name);
$tr.find(“td:eq(1)”).text(n.U_Name);
$tr.find(“td:eq(2)”).text(n.U_Age);
$tr.find(“td:eq(3)”).text(n.U_Sex);
$(“#View”).append($tr);
});
$(“#View”).find(“tr.Demo”).remove();//移除空行
});
});
});
</script>
</head>

<body>
<input type=”button” id=”btn” value=”顯示” />
<table width=”360″ border=”1″ id=”View”>
<tr>
<td align=”center”><strong>ID</strong></td>
<td align=”center”><strong>姓名</strong></td>
<td align=”center”><strong>年齡</strong></td>
<td align=”center”><strong>性別</strong></td>
</tr>
<tr class=”Demo”>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>

ASP利用JSON官方類輸出JSON就是這么簡單。

本例在Windows 2008R2+MySQL5.5+ASP環境下測試通過。

http://code.google.com/p/aspjson/ 有詳細的說明和下載,希望本文對您的ASP編程有所幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM