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个回答

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

这样写

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问