Asp生成Json类的方法和应用
我们在使用json时,很多时候都要用asp后台直接生成json格式的数据。
其实只要你明白json的格式,那就按照你需要的json去动态生成就好了,
下面是一个生成json的例子,大家可以参考下:
Class JSONClass
' 定义类属性,默认为Private
Dim SqlString ' 用于设置Select
Dim JSON ' 返回的JSON对象的名称
Dim DBConnection ' 连接到数据库的Connection对象
' 可以外部调用的公共方法
Public Function GetJSON ()
dim Rs
dim returnStr
dim i
dim oneRecord
' 获取数据
Set Rs= Server.CreateObject("ADODB.Recordset")
Rs.open SqlString,DBConnection,1,1
' 生成JSON字符串
if Rs.eof=false and Rs.Bof=false then
returnStr="{ "& JSON & ":{ records:["
while Rs.eof=false
' -------
oneRecord= "{"
for i=0 to Rs.Fields.Count -1
oneRecord=oneRecord &Rs.Fields(i).Name&":"
oneRecord=oneRecord & chr(34) &Rs.Fields(i).Value&chr(34) &","
Next
'去除记录最后一个字段后的","
oneRecord=left(oneRecord,InStrRev(oneRecord,",")-1)
oneRecord=oneRecord & "},"
'------------
returnStr=returnStr & oneRecord
Rs.MoveNext
Wend
' 去除所有记录数组后的","
returnStr=left(returnStr,InStrRev(returnStr,",")-1)
returnStr=returnStr & "]}}"
end if
Rs.close
set Rs=Nothing
GetJSON=returnStr
End Function
'私用方法,在类中使用
Private Function check()
End Function
End Class
dim a
set a=new JSONClass
a.Sqlstring="select * from aimks where name='mk'"
a.dbconnection=conn
a.json="aimks"
response.Write(a.GetJSon())
上面就是一个简单的动态生成json的asp类。你可以根据自己的需求进行更改。
Asp数据库二维数组rs.getrows方法的应用 Excel多窗口显示的设置方法
Your article helped me a lot, is there any more related content? Thanks!
学习了。真的很有用。~
谢谢。