| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2997 人关注过本帖, 1 人收藏
标题:读取以ASP文件生成的JSON数据问题
只看楼主 加入收藏
newman7237
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-30
结帖率:0
收藏(1)
已结贴  问题点数:20 回复次数:4 
读取以ASP文件生成的JSON数据问题
//后台Jos_test.asp
<%
Session.CodePage=65001
Response.CharSet="utf-8"
%>
<%
'---------------------------------------
' 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)
            returnStr="{ "&Chr(13)& Chr(9) & Chr(9) & """" & Root & """" & ": "& Chr(13) & Chr(9) & Chr(9) &Chr(9) & Chr(9) &"[ " & 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) & "}"
             returnStr=returnStr & Chr(9) & Chr(9) &Chr(9) & Chr(9) &"]" & Chr(13) & Chr(9) & Chr(9) & "}" &Chr(13) & ""
        End If
       response.Write returnStr
        GetJSON=returnStr
    End Function
   
    '私用方法,在类中使用
    Private Function check()
   
    End Function
   
    '数据库操作
    Sub initConn(conn)
        conn="Provider=SQLOLEDB;server=(local);UID=sa;PWD=U22117025u;database=hr"
        
        '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
%>
生成出来为:
test.rar (193.33 KB)

//前台ASP
<html>
 <head>   
 <title>JsonTest</title>  
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  <script type="text/javascript">
      var xmlHttp;
      function createXMLHttpRequest() {
          if (window.ActiveXObject) {
              xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          } else if (window.XMLHttpRequest) {
              xmlHttp = new XMLHttpRequest();
          }
      }
      function startRequest() {
          createXMLHttpRequest();
          try {
             xmlHttp.onreadystatechange = handleStateChange;
              xmlHttp.open("GET", "jos_test.asp", true);
              xmlHttp.send(null);
          } catch (exception) {
              alert("xmlHttp Fail");
          }
      }
   


      function handleStateChange() {
          if (xmlHttp.readyState == 4) {
              if (xmlHttp.status == 200 || xmlHttp.status == 0) {
                  var result = xmlHttp.responseText;
                  alert(result);
                  var json = eval("(" + result + ")");
            
                  alert(json.kquser[0].gh + ',' + json.kquser[0].m10);
                  alert(json.kquser[1].gh + ',' + json.kquser[1].m10);
                              }
           }
       }   

  </script>
 
  </head>
  
       <body>
         <div>    <input type="button" value="JSON Test" onclick="startRequest();" />  </div>   
           </body>
           </html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head>
    <title>Untitled Page</title>
</head>
<body>
<%
Set json = new JSONClass
json.Sql = "select gh,m10 from kquser where bm_bh='02' order by bzid"
json.Root = "kquser"

Call json.GetJSON()
'response.Write json.GetJSON()
%>
</body>
</html>
////////
如何使alert(json.kquser[0].gh + ',' + json.kquser[0].m10);
                  alert(json.kquser[1].gh + ',' + json.kquser[1].m10); 获取数据!!
搜索更多相关主题的帖子: 公安 网站 
2013-01-30 18:15
newman7237
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-30
收藏
得分:0 
如何去掉ASP文件的格式,而读取其中的JSON数据!!!急!!!
2013-01-30 18:16
newman7237
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-30
收藏
得分:0 
~~~~ding
2013-01-30 22:17
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3452
专家分:19340
注 册:2012-3-31
收藏
得分:20 
看看

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2013-02-04 19:38
xdsnet
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2013-2-27
收藏
得分:0 
------------------------------你原来供调用的asp--------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head>
    <title>Untitled Page</title>
</head>
<body>
<%
Set json = new JSONClass
json.Sql = "select gh,m10 from kquser where bm_bh='02' order by bzid"
json.Root = "kquser"

Call json.GetJSON()
'response.Write json.GetJSON()
%>
</body>
</html>
------------------------------你原来供调用的asp改成--------------------------------------------------------------------
<%
Set json = new JSONClass
json.Sql = "select gh,m10 from kquser where bm_bh='02' order by bzid"
json.Root = "kquser"

Call json.GetJSON()
'response.Write json.GetJSON()
%>
2013-02-27 14:44
快速回复:读取以ASP文件生成的JSON数据问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018966 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved