Any good libraries for parsing JSON in Classic ASP? [closed]
Solution 1:
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 https://github.com/douglascrockford/JSON-js/blob/master/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" src='path/to/json2.js'></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
%>
Solution 2:
Not sure about it. Have you checked ASP extreme framework which has JSON support?
Solution 3:
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
Solution 4:
I have recently implemented a VbsJson class, which has a "Decode" method to parse JSON to VBScript and a "Encode" method to generate JSON from VBScript. The code is somewhat long, so I don't paste it here.
Solution 5:
I wrote this answer when I was looking for a light-weight pure VBScript only solution.
By putting together a rudimentary JSON to XML converter, we can walk the JSON string and turn it into a Microsoft.XMLDOM document.
From there, we use Microsoft's XML API including XPath queries to pluck out any values we wanted.
This handles simple JSON, but, I never intended this answer for anything more sophisticated.
For a more robust solution, the best JSON interpreter, is a proper Javascript engine. Therefore, I highly recommend the accepted answer to this question i.e. Any good libraries for parsing JSON in Classic ASP?
Function JSONtoXML(jsonText)
Dim idx, max, ch, mode, xmldom, xmlelem, xmlchild, name, value
Set xmldom = CreateObject("Microsoft.XMLDOM")
xmldom.loadXML "<xml/>"
Set xmlelem = xmldom.documentElement
max = Len(jsonText)
mode = 0
name = ""
value = ""
While idx < max
idx = idx + 1
ch = Mid(jsonText, idx, 1)
Select Case mode
Case 0 ' Wait for Tag Root
Select Case ch
Case "{"
mode = 1
End Select
Case 1 ' Wait for Attribute/Tag Name
Select Case ch
Case """"
name = ""
mode = 2
Case "{"
Set xmlchild = xmldom.createElement("tag")
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
Case "["
Set xmlchild = xmldom.createElement("tag")
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
Case "}"
Set xmlelem = xmlelem.parentNode
Case "]"
Set xmlelem = xmlelem.parentNode
End Select
Case 2 ' Get Attribute/Tag Name
Select Case ch
Case """"
mode = 3
Case Else
name = name + ch
End Select
Case 3 ' Wait for colon
Select Case ch
Case ":"
mode = 4
End Select
Case 4 ' Wait for Attribute value or Tag contents
Select Case ch
Case "["
Set xmlchild = xmldom.createElement(name)
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
name = ""
mode = 1
Case "{"
Set xmlchild = xmldom.createElement(name)
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
name = ""
mode = 1
Case """"
value = ""
mode = 5
Case " "
Case Chr(9)
Case Chr(10)
Case Chr(13)
Case Else
value = ch
mode = 7
End Select
Case 5
Select Case ch
Case """"
xmlelem.setAttribute name, value
mode = 1
Case "\"
mode = 6
Case Else
value = value + ch
End Select
Case 6
value = value + ch
mode = 5
Case 7
If Instr("}], " & Chr(9) & vbCr & vbLf, ch) = 0 Then
value = value + ch
Else
xmlelem.setAttribute name, value
mode = 1
Select Case ch
Case "}"
Set xmlelem = xmlelem.parentNode
Case "]"
Set xmlelem = xmlelem.parentNode
End Select
End If
End Select
Wend
Set JSONtoXML = xmlDom
End Function
The above script, transforms the following JSON:
{
"owningSystemUrl": "http://www.arcgis.com",
"authInfo": {
"tokenServicesUrl": "https://www.arcgis.com/sharing/rest/generateToken",
"isTokenBasedSecurity": true
}
}
into:
<xml owningSystemUrl="http://www.arcgis.com">
<authInfo
tokenServicesUrl="https://www.arcgis.com/sharing/rest/generateToken"
isTokenBasedSecurity="true" >
</authInfo>
</xml>
We can now use XPath to extract the tokenServicesUrl
, for example:
dom.SelectSingleNode("xml/authInfo").getAttribute("tokenServicesUrl")
' Returns: "https://www.arcgis.com/sharing/rest/generateToken"