asp将数据库里的记录转换成json
[ 2009-11-16 09:37:48 | 作者: admin ]
<%
'---------------------------------------
' jsonclass类
' 将select语句的执行结果转换成json
'------------------------------------------
class jsonclass
' 定义类属性,默认为private
dim p_sqlstring ' 用于设置select
dim p_root ' 返回的json对象的名称
dim 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)
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) & "}"
end if
getjson=returnstr
end function
'私用方法,在类中使用
private function check()
end function
'数据库操作
sub initconn(conn)
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
%>
<%
set json = new jsonclass
json.sql = "select * from research_dictionary"
json.root = "research_dictionary"
response.charset="utf-8"
'call output(json.getjson())
%>
'---------------------------------------
' jsonclass类
' 将select语句的执行结果转换成json
'------------------------------------------
class jsonclass
' 定义类属性,默认为private
dim p_sqlstring ' 用于设置select
dim p_root ' 返回的json对象的名称
dim 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)
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) & "}"
end if
getjson=returnstr
end function
'私用方法,在类中使用
private function check()
end function
'数据库操作
sub initconn(conn)
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
%>
<%
set json = new jsonclass
json.sql = "select * from research_dictionary"
json.root = "research_dictionary"
response.charset="utf-8"
'call output(json.getjson())
%>
[最后修改由 admin, 于 2009-11-16 09:38:30]
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1321
这篇日志没有评论。
此日志不可发表评论。