您的位置:首页 > 编程语言 > ASP

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

2013-09-11 18:10 561 查看
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

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: