Json

This Class goal is to provide a simple way to parse JSON (JavaScript Object Notation) data directly from vbscript.

Notes

About

Summary
This Class goal is to provide a simple way to parse JSON (JavaScript Object Notation) data directly from vbscript.
Since ASP Classes strangely doesn’t accept parameters at it’s initializa- tion.
This function takes a dot separated path and look for the element value in the JSON.
Look for all element child keys and enumerate them.

Functions

loadJson

public sub loadJson( sJson )

Since ASP Classes strangely doesn’t accept parameters at it’s initializa- tion.  Use this subroutine to load the object.

Parameters

(string)sJson The Json string representation

getElement

public function getElement( sPath )

This function takes a dot separated path and look for the element value in the JSON.

Parameters

(string)sPath Relative path from root

Returns

(string) The element value

Example

dim sJson, oJson
 
sJson = "" & _
"{" & vbCrLf & _
"    'hello' : 'Hello World !'," & vbCrLf & _
"    'howdy' : 'How do you do ?'," & vbCrLf & _
"    'fields' : {" & vbCrLf & _
"       'one': 1," & vbCrLf & _
"       'two': 2," & vbCrLf & _
"       'three': 3" & vbCrLf & _
"    }" & vbCrLf & _
"};"
 
set oJson = new Json
oJson.loadJson(sJson)
Response.write(oJson.getElement("hello") & "<br />" & vbCrLf)
Response.write(oJson.getElement("howdy") & "<br />" & vbCrLf)
set oJson = nothing

getChildNodes

public function getChildNodes( sPath )

Look for all element child keys and enumerate them.

Parameters

(string)sPath Path to the parent element relative to root.

Returns

(string[]) With the child keys

Example

dim sJson, oJson, key
 
sJson = "" & _
"{" & vbCrLf & _
"    'hello' : 'Hello World !'," & vbCrLf & _
"    'howdy' : 'How do you do ?'," & vbCrLf & _
"    'fields' : {" & vbCrLf & _
"       'one': 1," & vbCrLf & _
"       'two': 2," & vbCrLf & _
"       'three': 3" & vbCrLf & _
"    }" & vbCrLf & _
"};"
 
set oJson = new Json
oJson.loadJson(sJson)
for each key in oJson.getChildNodes("")
    Response.write(key & " : " & oJson.getElement(key) & "<br />" & vbCrLf)
next
set oJson = nothing
public sub loadJson( sJson )
Since ASP Classes strangely doesn’t accept parameters at it’s initializa- tion.
public function getElement( sPath )
This function takes a dot separated path and look for the element value in the JSON.
public function getChildNodes( sPath )
Look for all element child keys and enumerate them.