ASP输出JSON数据及客户端jQuery处理方法

简介: 首先ASP处理JSON需要json官方提供的JSON For ASP 封装类文件,下载地址:http://code.google.com/p/aspjson/downloads/list 下载最新的JSON_2.0.4.asp文件备用。

首先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编程有所帮助。

目录
相关文章
|
1月前
|
JSON 前端开发 Java
Json格式数据解析
Json格式数据解析
|
4月前
|
JSON PHP 数据格式
|
2月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
3月前
|
XML 机器学习/深度学习 JSON
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
31 0
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
|
4天前
|
XML JSON API
转Android上基于JSON的数据交互应用
转Android上基于JSON的数据交互应用
|
11天前
|
JSON JavaScript Java
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
从前端Vue到后端Spring Boot:接收JSON数据的正确姿势
22 0
|
14天前
|
JSON 数据格式 Python
Python标准库中包含了json模块,可以帮助你轻松处理JSON数据
【4月更文挑战第30天】Python的json模块简化了JSON数据与Python对象之间的转换。使用`json.dumps()`可将字典转为JSON字符串,如`{&quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30, &quot;city&quot;: &quot;New York&quot;}`,而`json.loads()`则能将JSON字符串转回字典。通过`json.load()`从文件读取JSON数据,`json.dump()`则用于将数据写入文件。
17 1
|
14天前
|
JSON 数据格式 Python
Python处理JSON数据
【4月更文挑战第30天】该内容介绍了Python处理JSON数据的三个方法:1)使用`json.loads()`尝试解析字符串以验证其是否为有效JSON,通过捕获`JSONDecodeError`异常判断有效性;2)通过`json.dumps()`的`indent`参数格式化输出JSON数据,使其更易读;3)处理JSON中的日期,利用`dateutil`库将日期转换为字符串进行序列化和反序列化。
23 4
|
17天前
|
存储 JSON 数据处理
|
18天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
19 0