=波波日志 > Asp/VBScript > ASP生成json字符串类库=

[转]ASP生成json字符串类库

  下面这个类库通过设置查询字符串和链接对象,获取查询字符串中选择出的列自动生成对应的json字符串。
+展开
-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


使用示例

+展开
-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

返回的json格式为
{magazineTab:{ records:[{"Price":"30"}]}}


来源:http://topic.csdn.net/u/20110620/11/7cf83ff1-44e2-48e9-8f7b-250a2abec192.html?seed=1373984786&r=73965305#r_73965305
类别:Asp/VBScript 作者:转载 日期:2011-06-21 【评论:0】 
 
暂时没有评论!
发表留言
  • *昵称:
  • 头像:
  • 电子邮件: [留下您的邮件,方便管理员回复您。]
  • 个人网站: *验证码:
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,©2012,桂ICP备05005887号 京公网安备1101055090