ASP - Printing JSON without last comma and with double quotation marks

2

I need to query the database, and it returns a JSON.

I tried using JSON for ASP from Google, but it gave some errors, so I decided to do it manually:

<!--#include file="conexao.asp"-->

<%
medico = replace(request("medico"),"'","")
crm = replace(request("crm"),"'","")

sql = "SELECT nome_pro, crm FROM medicos WHERE nome LIKE UPPER('%" & medico & "%') "
set rs = server.createobject("adodb.recordset")
rs.cursorlocation = 3
rs.open sql,conexao


response.write("[")
If Not RS.EOF Then
Do
    response.write("{")
    for each x in rs.fields

        response.write("'" & x.name & "'")
        response.write(":")
        response.write("'" & x.value & "'")
        response.write(",") 

    next
    response.write("}")
    response.write(",")
    RS.MoveNext()

Loop Until RS.EOF
End If   
response.write("]")
%> 

The problem I'm having is that when it prints the JSON, it leaves the last comma, like this:

'PEDRO', 'CRM': '000000',}, {'PRIVATE_NAME': 'PEDRO', 'PEDRO', ' , 'CRM': '111111',},]

I've never used ASP, I'm using it for the first time to generate this JSON, and I wanted to know if you have any method of not printing this last comma.

And also wanted to know if you have how to make the:

response.write("'" & x.name & "'")

Print double quotes instead of single print.

    
asked by anonymous 01.06.2016 / 16:11

2 answers

0

Instead of writing directly, store the result in a variable, and at the end of the process, remove the comma at the end and then write the result.

See:

<!--#include file="conexao.asp"-->

<%
medico = replace(request("medico"),"'","")
crm = replace(request("crm"),"'","")

sql = "SELECT nome_pro, crm FROM medicos WHERE nome LIKE UPPER('%" & medico & "%') "
set rs = server.createobject("adodb.recordset")
rs.cursorlocation = 3
rs.open sql,conexao

Dim resposta

resposta = "["

If Not RS.EOF Then
Do
    resposta = resposta & "{"

    for each x in rs.fields

        resposta = resposta & "'" & x.name & "'"
        resposta = resposta & ":"
        resposta = resposta & "'" & x.value & "'"
        resposta = resposta & ","

    next

    resposta = resposta & "}"
    resposta = resposta & ","

    RS.MoveNext()

Loop Until RS.EOF
End If   

'Aqui removemos a ultima virgula no final da string.'
resposta = Left(resposta, (len(resposta)-1))

resposta = resposta & "]"

response.write resposta
%> 
    
05.07.2016 / 16:12
0

I will only show the ceiling of the loop, because it is only in this section that I modified:

c2 = 0;
Do
    If c2 > 0 Then
        response.write(",")
    End If
    response.write("{")
    c = 0;
    for each x in rs.fields
        If c > 0 Then
            response.write(",")
        End If
        response.write("'" & x.name & "'")
        response.write(":")
        response.write("'" & x.value & "'")
        c = 1
    next
    response.write("}")
    c2 = 1;
    RS.MoveNext()
Loop Until RS.EOF

The logic is to add the comma before and allow only when the counter is greater than 0, ie when the loopback is running the second record on.

Note that the c2 and c counter exists.

c2 is for the primary loop.

Inside the primary loop there is another loop, where I used a variable called c .

The important thing is to start counters with a value of 0 before starting each loop, not inside the loop.

Also beware of reserved json characters. It is safer to escape strings if they contain reserved characters such as double quote, for example.

Replace(str, ""","\"")

Practical example

response.write("'" & Replace(x.name, ""","\"") & "'")
    
05.07.2016 / 16:49