ASP生成json字符串类库
下面这个类库通过设置查询字符串和链接对象,获取查询字符串中选择出的列自动生成对应的json字符串。
使用示例
返回的json格式为
{magazineTab:{ records:[{"Price":"30"}]}}
来源:http://topic.csdn.net/u/20110620/11/7cf83ff1-44e2-48e9-8f7b-250a2abec192.html?seed=1373984786&r=73965305#r_73965305
+展开
-VBScript
Class JSONClass
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 & 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 & "},"
'------------
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 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 & 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 & "},"
'------------
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
使用示例
+展开
-VBScript
dim a ,json
set a=new JSONClass
a.Sqlstring="Select Price From Products where id=1"
a.dbconnection=conn
a.json="magazineTab"
json=a.GetJSON()'获取json字符串
response.write json
set a=new JSONClass
a.Sqlstring="Select Price From Products where id=1"
a.dbconnection=conn
a.json="magazineTab"
json=a.GetJSON()'获取json字符串
response.write json
返回的json格式为
{magazineTab:{ records:[{"Price":"30"}]}}
来源:http://topic.csdn.net/u/20110620/11/7cf83ff1-44e2-48e9-8f7b-250a2abec192.html?seed=1373984786&r=73965305#r_73965305
加支付宝好友偷能量挖...