asp将数据库数据转换成JSON格式

来源:互联网 发布:linux 局域网上网设置 编辑:程序博客网 时间:2024/05/17 18:15

asp从数据库中读取数据,以JSON形式输出,作为Android 的WebService

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="conn.asp" -->
<%
response.Charset = "gb2312"
Dim nPageSize,strCatID,strCatName,strInfoView,strInfoFile,nPageCount
strCatID=request("ChannelID")
nPageSize=request("PageSize")
If not Isnumeric(strCatID) Then
 strCatID=0
End If
If not Isnumeric(nPageSize) Then
    nPageSize=0
End If
If strCatID<=0 Then
    Response.Write "参数错误!"
 Response.End
End If
If nPageSize<=0 Then
    Response.Write "参数错误!"
 Response.End
End If
cmdQuery.CommandText = "select top "&nPageSize&" nid,title,source,digest,ptime from t_news where cid='"&strCatID&"' order by nid desc"
Set rstCat=cmdQuery.Execute
If not rstCat.Eof Then
    response.Write "{'ret':0,'data':{'newslist':["
 for i=1 to nPageSize
  response.Write "{"
  response.Write "'title':'"&rstCat("title")&"',"
  Set cmdQuery2=Server.CreateObject("ADODB.Command")
  Set cmdQuery2.ActiveConnection = cnnMyedu123
  cmdQuery2.CommandType=1
  cmdQuery2.CommandText = "select * from ViewCommentCount where nid="&rstCat("nid")
  Set rstCount=cmdQuery2.Execute
  if not rstCount.eof then
   response.Write "'commentcount':"&rstCount("commentcount")&","
  else
   response.Write "'commentcount':0,"
  end if
  set rstCount = nothing
  set cmdQuery2 = nothing
  response.Write "'source':'"&rstCat("source")&"',"
  response.Write "'nid':"&rstCat("nid")&","
  response.Write "'digest':'"&rstCat("digest")&"',"
  response.Write "'ptime':'"&rstCat("ptime")&"'"
  if i = nPageSize-1 then
   response.Write "}"
  else
   response.Write "},"
  end if
  rstCat.movenext
  if rstCat.eof then exit for end if
 next
 response.Write "],"
 response.Write "'totalnum':"&nPageSize&""
 response.Write "},"
 response.Write "'msg':'ok'"
 response.Write "}"
Else
    Response.Write "栏目不存在!"
 Response.End
End If
Set rstCat=Nothing
set cmdQuery = nothing
%>