When working with JSON it's nice to have a generator for your language which transforms all the datatypes from your chosen programming language into the JSON grammar so that you can use them within javascript. For a lot of popular languages it's done already (i suppose) but I haven't found one for classic ASP .. so here it comes. The following example quickly demonstrates what the goal is:
ASP:
-
set RS = getRecordset("SELECT * FROM table")
-
response.write((new JSON).toJSON("rows", RS))
A simple usage of JSON normally is that you create a page which outputs data as JSON as the only response. This page is called later from another page and the returned data is used within a javascript function (known as callback). So the snippet above gets some data from the database and stores it in an adodb.recordset which is passed then to the JSON generator and the result is printed on the page. The consuming page would like to access the data now as it originally was within the recordset. like this:
JAVASCRIPT:
-
function callback(rows) {
-
for (i = 0; i <rows.length; i++) {
-
alert(rows[i].columName);
-
}
-
}
read on to get the details... (more...)