!!.默默
2019-05-12 20:45
采纳率: 53.8%
浏览 479
已采纳

ASP从数据库直接输出json

ASP从数据库直接输出json 多行以,区分最后一行不加,该如何修改呢

([<%
dim conn,connstr,rs,sql
set conn = Server.CreateObject("Adodb.Connection")
connstr = "Provider=SQLOLEDB;data source=127.0.0.1;initial catalog=数据库;uid=数据库用户;pwd=数据库密码"
conn.open connstr
set rs = Server.CreateObject("Adodb.RecordSet")
sql = "select Name,userid from user"
rs.open sql,conn,1,1
While Not rs.EOF
Name = rs("Name") 
userid = rs("userid") 
response.write "{""Name"":""*"&Name&""",""userid"":"&userid&"}"
if not rs.eof Then
Response.Write ","
end if
rs.MoveNext
Wend
rs.close
conn.close
set conn=nothing 
%>])
输出的结果为:([{"Name":"用户","userid":1},{"Name":"用户","userid":1},]) 
想要的输出结果([{"Name":"用户","userid":1},{"Name":"用户","userid":1}])
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2019-05-13 08:53
    已采纳

    if not rs.eof Then
    Response.Write ","
    end if
    这个条件在遇到最后一条记录的时候也满足,也会输出逗号。

    dim firstrec
    firstrec = true
    While Not rs.EOF
    Name = rs("Name")
    userid = rs("userid")
    if firstrec = true then
    firstrec = false
    else
    Response.Write ","
    end if
    response.write "{""Name"":"""&Name&""",""userid"":"&userid&"}"
    rs.MoveNext
    Wend

    这样写

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题