Asp数据转Json

需要引用的文件:

json.asp(可在JSON官网下载,也可在底部链接的demo中直接拷贝该文件)

Conn.asp是链接数据库文件

<%@LANGUAGE="%>
<% Response.Addheader "Content-Type","text/html; charset=utf-8"  %>
<!--#include file="inc/json.asp"-->
<!--#include file="inc/Conn.asp" -->

组拼Sql(需要修改):

<%
    Dim sql_class,sql_top,sql_colums,sql_whereBy,sql_orderBy
    sql_class = request.Item("sql_class")
    sql_top = request.Item("sql_top")
    sql_colums = request.Item("sql_colums")
    sql_whereBy = request.Item("sql_whereBy")
    sql_orderBy = request.Item("sql_orderBy")

    Sql="select "&sql_top&" "&sql_colums&" from "&sql_class&" where 1=1 "&sql_whereBy&" "&sql_orderBy
%>

输出Json数据(无需修改):

<%
    Set Rs = Server.CreateObject("ADODB.Recordset")
    Rs.Open sql,conn,,
    jsonStr = ""
    rows = ""

    Dim i,json_rows,json_ret,arr_rows
    Dim myArray()
    ) '将数组大小重新定义为20
    Set jsonObj=New json
    jsonObj.toResponse=False
    Set json_ret = server.createobject("scripting.dictionary")        

        Set myArray(i) = server.createobject("scripting.dictionary")
        For Each e In rs.Fields
                'rows = rows &""""& e.Name & """:""" & replace(e.value,chr(34),"@_'_@") & ""","
                myArray(i).Add e.Name,e.value  '将key/value加到行数组对象中
        Next
        Rs.movenext
    Next
    json_ret.Add "total",rs.recordcount
    json_ret.Add "rows",myArray
    jsonStr = jsonObj.toJSON(Empty,json_ret,False)

    response.Write jsonStr

%>

使用很简单,只需要修改sql语句即可

demo文件链接:

http://files.cnblogs.com/files/LucklyPisces/AspToJson.rar

上一篇:「luogu2387」[NOI2014] 魔法森林


下一篇:C++ namespace功能总结