asp自定义函数怎么返回JSON数据?是asp,不是asp.net
展开全部
<%
Response.CharSet="gb2312"
Function GetJson(Table,Column,Where,PrimKey,OrderBy,CurPage,PageSizes)
'使用方法 : Response.Write GetJson(Table表名,Column字段,Where条件,PrimKey主键(必须:通常为ID),OrderBy(id desc),CurPage当前页,PageSizes每页条数)
dim i, j ,rs
if OrderBy="" Then OrderBy="id desc"
if Where="" Then Where="id<>''"
Set rs=Server.CreateObject("adodb.recordset")
TmpSql = "select " & Column & " from " & Table & " where " & Where & " order by " & OrderBy
if PageSizes=0 Then TmpSql = "select " & Column & " from " & Table & " where " & Where & " order by " & OrderBy
rs.open TmpSql,conn,1,1
rs.pagesize=PageSizes
page=clng(CurPage)
if page<1 then page=1
if page>rs.pagecount then page=rs.pagecount
rs.Absolutepage=page
if rs.recordcount<=0 then
Response.Write("noRecords")
else
if page*PageSizes<rs.recordcount then
for i=0 to rs.pagesize-1
TmpJson= TmpJson & "{"
for j=0 to rs.fields.count-1
TmpJson= TmpJson & """" &(rs.fields(j).name) & """:"
TmpJson= TmpJson & """" & rs(j) & """"
if j<rs.fields.count-1 then TmpJson= TmpJson & ","
next
TmpJson= TmpJson & "}"
if i<rs.pagesize-1 then TmpJson= TmpJson & ","
TmpJson= TmpJson
rs.movenext
next
elseif page*PageSizes>=rs.recordcount then
for i=0 to rs.recordcount-1-(page-1)*PageSizes
TmpJson= TmpJson & "{"
for j=0 to rs.fields.count-1
TmpJson= TmpJson & """" &(rs.fields(j).name) & """:"
TmpJson= TmpJson & """" & rs(j) & """"
if j<rs.fields.count-1 then TmpJson= TmpJson & ","
next
TmpJson= TmpJson & "}"
if i<rs.recordcount-1-(page-1)*PageSizes then TmpJson= TmpJson & ","
TmpJson= TmpJson
rs.movenext
next
end if
GetJson=toJson(TmpJson)
end if
rs.close
Set rs=nothing
End Function
Function toJson(JsonStr)
toJson="["& JsonStr &"]"
End Function
'使用
Response.Write GetJson("studentInfo","id,country","flag=1","id","id desc",1,20)
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询