52,797
社区成员
发帖
与我相关
我的任务
分享
<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("date.mdb")
%>
<%
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 a
set a=new JSONClass
a.Sqlstring="Select Price From Products where id=1"
a.dbconnection=conn
a.json="magazineTab"
%>
{
"add_to_box": {
"action": "/boxes/update_or_create",
},
"calculation": {"xlink:href":"","estimated_value":这里获取上面查询结果的Price的值,"product":{"name":"Iconia Tab A500 16GB 10.1 Tablet ","product_type":"Tradeable","json_class":"Product","manufacturer_name":"","calculator_url":""}},
}
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()'获取price的json字符串
response.Write"{ ""add_to_box"": { ""action"": ""/boxes/update_or_create"", }, ""calculation"": {""estimated_value"":"&json&",""json_class"":""Product""}}, }"
{
"add_to_box": {
"action": "/boxes/update_or_create",
},
"calculation": {"estimated_value":这里获取上面查询结果的Price的值,"json_class":"Product"}},
}
price="0.0"'默认一个值
set rs=conn.execute("Select Price From Products where id=1")'获取对应的价格
if not (rs.eof or rs.bof) then price=rs(0)'记录存在
response.Write"{ ""add_to_box"": { ""action"": ""/boxes/update_or_create"", }, ""calculation"": {""estimated_value"":"&price&",""json_class"":""Product""}}, }"
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
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()'获取price的json字符串
'连接代码不用写了吧?。。。注意asp双引号的转义就行了
'如果不是使用json1.4+版本,键名称可以不使用双引号括起的,或者改成单引号,这样就不需要转义这么麻烦了