asp将数据库里的记录转换成json

来源:互联网 发布:windows的搜索功能 编辑:程序博客网 时间:2024/05/19 10:40
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
<%
'--------------------------------------- 
'
 JSONClass类 
'
 将Select语句的执行结果转换成JSON 
'
------------------------------------------ 
Class JSONClass 
    
' 定义类属性,默认为Private 
    Private p_SqlString ' 用于设置Select 
    Private p_root ' 返回的JSON对象的名称 
    Private Rs,conn
    
    
Private Sub Class_Initialize()
        SqlString 
= ""
        JSON 
= ""
        
'初始化conn和rs
        Call initConn(conn)
        
Call initRs(rs) 
    
End Sub
    
    
Private Sub Class_Terminate()
        
'清除conn和rs
        Call clearConn(conn)
        
Call clearRs(rs)
    
End Sub
    
    
' 可以外部调用的公共方法 
    Public Function GetJSON()
        
Dim Rs 
        
Dim returnStr 
        
Dim i 
        
Dim oneRecord 
        
        
' 获取数据 
        Set Rs= Server.CreateObject("ADODB.Recordset"
        Rs.open Sql,conn,
1,1 
        
' 生成JSON字符串 
        If Rs.eof=false And Rs.Bof=false Then 
            returnStr
=""&Chr(13)& Chr(9& Chr(9& Root & ":{ "& Chr(13& Chr(9& Chr(9&Chr(9& Chr(9&"records:[ " & Chr(13)
            
            
While(Not Rs.Eof)
                
' ------- 
                oneRecord= Chr(9& Chr(9& Chr(9& Chr(9& Chr(9& "" 
                
                
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 & "}," & Chr(13)
            
'------------ 
            returnStr=returnStr & oneRecord 
            Rs.MoveNext 
            
Wend 
            
' 去除所有记录数组后的"," 
            returnStr=Left(returnStr,InStrRev(returnStr,",")-1& Chr(13)
            returnStr
=returnStr & Chr(9& Chr(9&Chr(9& Chr(9&"]" & Chr(13& Chr(9& Chr(9& "}" &Chr(13& "}" 
        
End If

        GetJSON
=returnStr 
    
End Function 
    
    
'私用方法,在类中使用 
    Private Function check() 
    
    
End Function 
    
    
'数据库操作
    Sub initConn(conn)
        
Set conn=Server.CreateObject("ADODB.Connection")
        conn.Mode
=3
        conn.Open connStr
    
End Sub
    
    
Sub clearConn(conn)
        conn.Close
        
Set conn=Nothing
    
End Sub
    
    
Sub initRs(rs)
        
Set Rs=Server.CreateObject("ADODB.RecordSet")
    
End Sub
    
Sub clearRs(Rs)
        
Set Rs=Nothing
    
End Sub
    
    
Public Property Get Sql
        Sql 
= p_SqlString
    
End Property
    
    
Public Property Let Sql(value)
        p_SqlString 
= value
    
End Property
    
    
Public Property Get Root
        Root 
= p_root
    
End Property
    
    
Public Property Let Root(value)
        p_root 
= value
    
End Property
' 
End Class 
%
>
<%
Set json = new JSONClass
json.Sql 
= "SELECT * FROM Research_Dictionary"
json.Root 
= "Research_Dictionary"

Response.Charset
="utf-8"

'Call OutPut(json.GetJSON())
%>
<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>
 
原创粉丝点击