读取以ASP文件生成的JSON数据问题
//后台Jos_test.asp<%
Session.CodePage=65001
Response.CharSet="utf-8"
%>
<%
'---------------------------------------
' JSONClass类
' 将Select语句的执行结果转换成JSON
'------------------------------------------
Class JSONClass
' 定义类属性,默认为Private
Private p_SqlString ' 用于设置Select
Private p_root ' 返回的JSON对象的名称
Private Rs,conn
Private Sub Class_Initialize()
SqlString = ""
JSON = ""
'初始化conn和rs
Call initConn(conn)
Call initRs(rs)
End Sub
Private Sub Class_Terminate()
'清除conn和rs
Call clearConn(conn)
Call clearRs(rs)
End Sub
' 可以外部调用的公共方法
Public Function GetJSON()
Dim Rs
Dim returnStr
Dim i
Dim oneRecord
' 获取数据
Set Rs= Server.CreateObject("ADODB.Recordset")
Rs.open Sql,conn,1,1
' 生成JSON字符串
If Rs.eof=false And Rs.Bof=false Then
'returnStr="{ "&Chr(13)& Chr(9) & Chr(9) & Root & ":{ "& Chr(13) & Chr(9) & Chr(9) &Chr(9) & Chr(9) &"records:[ " & Chr(13)
returnStr="{ "&Chr(13)& Chr(9) & Chr(9) & """" & Root & """" & ": "& Chr(13) & Chr(9) & Chr(9) &Chr(9) & Chr(9) &"[ " & Chr(13)
While(Not Rs.Eof)
' -------
oneRecord= Chr(9) & Chr(9) & Chr(9) & Chr(9) & Chr(9) & "{ "
For i=0 To Rs.Fields.Count -1
oneRecord=oneRecord & Chr(34) & Rs.Fields(i).Name&Chr(34) &":"
oneRecord=oneRecord & Chr(34) & Rs.Fields(i).Value&Chr(34) &","
Next
'去除记录最后一个字段后的","
oneRecord=Left(oneRecord,InStrRev(oneRecord,",")-1)
oneRecord=oneRecord & "}," & Chr(13)
'------------
returnStr=returnStr & oneRecord
Rs.MoveNext
Wend
' 去除所有记录数组后的","
returnStr=Left(returnStr,InStrRev(returnStr,",")-1) & Chr(13)
'returnStr=returnStr & Chr(9) & Chr(9) &Chr(9) & Chr(9) &"]" & Chr(13) & Chr(9) & Chr(9) & "}" &Chr(13) & "}"
returnStr=returnStr & Chr(9) & Chr(9) &Chr(9) & Chr(9) &"]" & Chr(13) & Chr(9) & Chr(9) & "}" &Chr(13) & ""
End If
response.Write returnStr
GetJSON=returnStr
End Function
'私用方法,在类中使用
Private Function check()
End Function
'数据库操作
Sub initConn(conn)
conn="Provider=SQLOLEDB;server=(local);UID=sa;PWD=U22117025u;database=hr"
'Set conn=Server.CreateObject("ADODB.Connection")
conn.Mode=3
conn.Open connStr
End Sub
Sub clearConn(conn)
conn.Close
Set conn=Nothing
End Sub
Sub initRs(rs)
Set Rs=Server.CreateObject("ADODB.RecordSet")
End Sub
Sub clearRs(Rs)
Set Rs=Nothing
End Sub
Public Property Get Sql
Sql = p_SqlString
End Property
Public Property Let Sql(value)
p_SqlString = value
End Property
Public Property Get Root
Root = p_root
End Property
Public Property Let Root(value)
p_root = value
End Property
'
End Class
%>
生成出来为:
test.rar
(193.33 KB)
//前台ASP
<html>
<head>
<title>JsonTest</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function startRequest() {
createXMLHttpRequest();
try {
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "jos_test.asp", true);
xmlHttp.send(null);
} catch (exception) {
alert("xmlHttp Fail");
}
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200 || xmlHttp.status == 0) {
var result = xmlHttp.responseText;
alert(result);
var json = eval("(" + result + ")");
alert(json.kquser[0].gh + ',' + json.kquser[0].m10);
alert(json.kquser[1].gh + ',' + json.kquser[1].m10);
}
}
}
</script>
</head>
<body>
<div> <input type="button" value="JSON Test" onclick="startRequest();" /> </div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head>
<title>Untitled Page</title>
</head>
<body>
<%
Set json = new JSONClass
json.Sql = "select gh,m10 from kquser where bm_bh='02' order by bzid"
json.Root = "kquser"
Call json.GetJSON()
'response.Write json.GetJSON()
%>
</body>
</html>
////////
如何使alert(json.kquser[0].gh + ',' + json.kquser[0].m10);
alert(json.kquser[1].gh + ',' + json.kquser[1].m10); 获取数据!!