user6461738
user6461738

Reputation:

Classic ASP fetch parameters from HTTP using JSON

I have website written using classic asp and i need to create a page that will receive a callback from a third party site which will be sending three parameters using JSON. I then need to store these values in a database.

I have no experience of JSON and need a way to capture the parameters in my asp page.

Anyone have any experience with JSON.

Thanks

Upvotes: 0

Views: 2137

Answers (1)

4532066
4532066

Reputation: 2110

I've use http://www.aspjson.com/ for using Classic ASP with JSON.

I downloaded the code from the site above, and have it as an include file on my page:

<!--#INCLUDE file="../dist/asp/aspJSON.asp" -->

Then I can parse through the JSON response and assign variables to it.

I've used it mainly for sending emails using the Mandrill Email API.

The API sends the response in JSON format.

Example response:

[
    {
        "email": "[email protected]",
        "status": "sent",
        "reject_reason": "hard-bounce",
        "_id": "abc123abc123abc123abc123abc123"
    }
]

Send data to Mandrill...

vurl = "https://mandrillapp.com/api/1.0/messages/send.json"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0") 
xmlhttp.open "POST", vurl, false 
xmlhttp.setRequestHeader "Content-type","application/json"
xmlhttp.setRequestHeader "Accept","application/json"

'send JSON data to the API
xmlhttp.send oJSON.JSONoutput()

Mandrill then sends a JSON response - e.g.

[
    {
        "email": "[email protected]",
        "status": "sent",
        "reject_reason": "hard-bounce",
        "_id": "abc123abc123abc123abc123abc123"
    }
]

I can then process it using:

'process the response JSON data
vAnswer = xmlhttp.responseText

I have to remove the square brackets from the start and end of the JSON response:

vAnswer = replace(vAnswer,"[","")
vAnswer = replace(vAnswer,"]","")

And then do stuff with the data:

'load the incoming JSON data using aspJSON

Set oJSON = New aspJSON

'Load JSON string
oJSON.loadJSON(vAnswer)

'set variable values from the incoming data

json_email =            ap(oJSON.data("email"))
json_status =           ap(oJSON.data("status"))
json_reject_reason =    ap(oJSON.data("reject_reason"))
json_id =               ap(oJSON.data("_id"))

How you would do it would depend on the structure of the JSON data you are working with.

Upvotes: 1

Related Questions