LDNDeveloper

Andrew Pallant

Software & Web Developer


Donate To Support My Blog Donate if this post helped you. Coffee money is always welcomed!




Tricking Out Classic ASP With JSON – Updated

Physical Link: Tricking Out Classic ASP With JSON – Updated


Amendment:  Some people have had trouble downloading the aspJSON.asp file from the site where I had found it.  You can download the file from me.

Amendment:: Some people were asking for a solution to Mandrill and SendGrid. Both solutions are similar and therefore I selected one to publish. You can contact me if you would like more. See the following: Part II – The Mandrill Experience

Sometimes you get trapped in using languages you don’t prefer. I often find my self playing in the bowels of Classic ASP. Now Classic ASP is not bad, but it is a little outdated and has not really been built upon for a few years. I have recently built some new tools in Classic ASP to compliment an existing tool set; however, I tricked out ASP with JSON.

To do this I had found a wonderful import file written by Gerrit van Kuipers. You can download a copy of the file from: www.aspjson.com. Yes it is version 1.0, but I have not found an issue with it yet.

At the very bottom of this article is the the site listed again along with a tool to validate JSON strings.

You will need to import the aspJSON.asp file in your ASP file.

     <!--#include virtual="/aspJSON.asp" -->

To read the JSON request you will need to read the binary of the request and convert it to a string.

    If Request.TotalBytes > 0 Then
        Dim lngBytesCount
        lngBytesCount = Request.TotalBytes
        jsonstring = BytesToStr(Request.BinaryRead(lngBytesCount))
    End If

    Function BytesToStr(bytes)
        Dim Stream
        Set Stream = Server.CreateObject("Adodb.Stream")
            Stream.Type = 1 'adTypeBinary
            Stream.Open
            Stream.Write bytes
            Stream.Position = 0
            Stream.Type = 2 'adTypeText
            Stream.Charset = "iso-8859-1"
            BytesToStr = Stream.ReadText
            Stream.Close
        Set Stream = Nothing
    End Function

Next you will need to create the JSON object to parse it.

     Set oJSON = New aspJSON
     oJSON.loadJSON(jsonstring)

Now you can access the data using the JSON object you created in the above step.

     FirstName = oJSON.data.item("FirstName")

To Write a JSON request you will need to do the following.

You will need to import the aspJSON.asp file in your ASP file.

     <!--#include virtual="/aspJSON.asp" -->

Next you will need to create the JSON object and populate it

     Set oJSON = New aspJSON
     set oJSON.data("employee")
     set newitem = oJSON.addToCollection(oJSON.data("employee"))
     newitem.add "FirstName", "Andrew"
     newitem.add "LastName", "Pallant"

Now you can submit your request

     Response.Clear
     Response.ContentType = "application/json"
     Response.Write oJSON.JSONoutput()

You can vist: http://www.aspjson.com/ for complete example and to download the aspJSON.asp file.

A tool that I use to validate the string file is: http://jsonformatter.curiousconcept.com/

Part II of the Article Has Just Been Added:  http://andrewpallant.ca/wordpress/tricking-classic-asp-json-mandrill-experience/

The post Tricking Out Classic ASP With JSON – Updated appeared first on LDNDeveloper.

Author:
Categories: ASP, Developement, JSON, Web, Classic ASP


©2024 LdnDeveloper