欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp实现读取数据库输出json代码

程序员文章站 2022-07-02 18:13:15
复制代码 代码如下: function gettable(table,where,order,ordersort,curpage, pagesize,minijson)...

复制代码 代码如下:

function gettable(table,where,order,ordersort,curpage, pagesize,minijson)
'author : nigou
'使用方法 : response.write gettable(table表名,where条件,order主键(必须),ordersort(asc,desc),curpage当前页, pagesize每页条数,minijson是否输出为miniui格式)
'
    dim i, j ,rs
    if ordersort="" then ordersort="asc"
    if where="" then where="asc"
    set rs=server.createobject("adodb.recordset")
    if curpage>1 then
        tmpsql="select a.* from ( select top  " & (curpage) * pagesize & " * from " & table & " where " & where  & " order by " & order & " " & ordersort & ") a left join ( select top " & (curpage-1) * pagesize  & " * from " & table & "  where " & where  & " order by " & order & " " & ordersort & ") b on a." & order & "=b." & order & " where iif(b." & order & ",'0','1')='1'"
    else
        tmpsql="select a.* from ( select top  " & (curpage) * pagesize & " * from " & table & " where " & where  & " order by " & order & " " & ordersort & ") a "
    end if
    if pagesize=0 then tmpsql = "select * from " & table
        rs.open tmpsql,conn,1,1
        realsize=rs.recordcount
        for i=0 to rs.recordcount-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.recordcount-1 then tmpjson= tmpjson & ","
            tmpjson= tmpjson & vbcrlf
            rs.movenext
        next
        rs.close
    if minijson=1 then
        countsql="select count("& order &") from " & table & " where " & where
        rs.open countsql,conn,1,1
        counts=rs(0)
        rs.close
        gettable=tominiui(tmpjson,counts)
    else
        gettable=tojson(tmpjson)
    end if
    set rs=nothing
end function
function tojson(jsonstr)
    tojson="[" & vbcrlf & jsonstr & vbcrlf & "]"
end function
function tominiui(jsonstr,total)
    tominiui="{"
    tominiui=tominiui & """total"":"" " & total & """," & vbcrlf
    tominiui=tominiui & """data"": [" & jsonstr
    tominiui=tominiui & "]}"
end function

ps:最后的参数是针对miniui开发的,可以无视

以上就是本文的全部内容了,希望大家能够喜欢。