views:

3512

answers:

5

I've been able to find a zillion libraries for generating JSON in Classic ASP (VBScript) but I haven't been to find ANY for parsing.

I want something that I can pass a JSON string and get back a VBScript object of some sort (Array, Scripting.Dictionary, etc)

Can anyone recommend a library for parsing JSON in Classic ASP?

+6  A: 

Not sure about it. Have you checked ASP extreme framework which has JSON support?

Shoban
You're my hero. That works perfectly! I'm going to take a look at the framework because it seems very handy but I was able to just lift out the JSON class and start using it by itself.
Mark Biek
wow.. Glad that I was able to help you ;-)
Shoban
I should mention though, that this JSON class seems to have trouble with Unicode.
Mark Biek
+8  A: 

Keep in mind that Classic ASP includes JScript as well as VBScript. Interestingly, you can parse JSON using JScript and use the resulting objects directly in VBScript.

Therefore, it is possible to use the canonical http://www.json.org/json2.js in server-side code with zero modifications.

Of course, if your JSON includes any arrays, these will remain JScript arrays when parsing is complete. You can access the contents of the JScript array from VBScript using dot notation.

<%@Language="VBScript" %>
<%
Option Explicit
%>
<script language="JScript" runat="server">
... insert contents of http://www.json.org/json2.js here ...
</script>
<%

Dim myJSON
myJSON = Request.Form("myJSON") // "[ 1, 2, 3 ]"
Set myJSON = JSON.parse(myJSON) // [1,2,3]
Response.Write(myJSON)          // 1,2,3
Response.Write(myJSON.[0])      // 1
Response.Write(myJSON.[1])      // 2
Response.Write(myJSON.[2])      // 3
%>
Chris Nielsen
A: 

Hi,

I am trying to use this link text

for JSON, but I could not find the file, can anyone help me to find the file to download..thx

Mark

Mark
+4  A: 

I couldn't get the extreme-evolution or Chris Nielson's suggestion to work. But, the following did work for me:

http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP

Download the following as "json2.min.asp"

http://tforster.wik.is/@api/deki/files/2/=json2.min.asp

Add the following line to the top of your ASP file:

<script language="javascript" runat="server" src="json2.min.asp"></script>

You can then use JSON in ASP.

   Dim car: Set car = JSON.parse("{""brand"":""subaru"",""model"":""outback sport"",""year"":2003," & _
                                 """colour"":""green"",""accessories"":[" & _
                                 "{""foglamps"":true},{""abs"":true},{""heatedSeats"":true}]}")

   Response.Write("brand: " & car.brand & "<br/>")                               
   Response.Write("model: " & car.model & "<br/>")                               
   Response.Write("colour: " & car.colour & "<br/>")                               
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               

   car.accessories.get(0).foglamps = false
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               
   Response.Write("new Json: " & JSON.stringify(car) & "<br/>")

   Set car = Nothing

Note: To parse through an array of items, you need to do the following:

   for each iTmp in testing
       if (TypeName(iTmp))<>"JScriptTypeInfo" then 
           Response.Write("Item: " &  iTmp & "<br/>")
       end if
   next
seanyboy