How to return a JSON object in classic ASP

前端 未结 3 735
慢半拍i
慢半拍i 2020-12-11 01:10

I want to return a JSON object using a classic ASP script (it\'s part of an AJAX request).

If I just send the reponse as text like:

response.write(\"         


        
相关标签:
3条回答
  • 2020-12-11 01:55

    Joe's answer should work for you. However you might want to look at aspjson if you are going to be outputting a lot of JSON from classic ASP.

    0 讨论(0)
  • 2020-12-11 02:06

    I got it to work with the code below.... After doubling the quotes and putting in the query string

    currQuery= request.querystring("query")
    response.expires=-1
    Dim rsMain,sqlMain,rettxt,JobOpenToArr
    set rsMain= Server.CreateObject("ADODB.Recordset")
    rsMain.CursorLocation = adUseClient
    sqlMain = "select JobOpenTo FROM Jobs WHERE JobOpenTo LIKE '%"&currQuery & "%' group by JobOpenTo order by JobOpenTo" 
    rsMain.Open sqlMain, Session("XXX_CMS")
    if Not rsMain.Eof  Then   
                  '## build the string
           rettxt = "{query:""" & currQuery & """, suggestions:["
    
         JobOpenToArr = rsMain.getRows()     
         For i = 0 to UBound(JobOpenToArr,2)
    
           rettxt = rettxt & """" & JobOpenToArr(0,i) & ""","
    
         Next    
         '##knock off trailing comma
         rettxt = left(rettxt,len(rettxt)-1)
         rettxt = rettxt & "]}"
         Response.Write rettxt 
    
    0 讨论(0)
  • 2020-12-11 02:16

    It appears to be a parsing error on the client side.

    I didn't think this would make a difference, but it looks like if you quote everything, including the property names, it seems to work. And use double-quotes instead of single quotes - that apparently is making a difference.

    Remember to double your double-quotes (at least I think that's how you do it in VBScript - been a long time).

    So:

    <%
        Response.ContentType = "application/json"
        Response.Write("{ ""query"":""Li"", ""suggestions"":[""Liberia"",""Libyan Arab Jamahiriya"",""Liechtenstein"",""Lithuania""], ""data"":[""LR"",""LY"",""LI"",""LT""] }")
    %>
    
    0 讨论(0)
提交回复
热议问题