4

What is the best way to parse json string into classic asp using a library?

Dim jsonString
jsonString = {"date":"4/28/2017","custType":"100","vehicle":"1"}

Would like to have

response.write("<li> date :" & json("date") & "</li>")
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
  • Possible duplicate of [Using VBscript to access all values in JSON data](http://stackoverflow.com/questions/41610139/using-vbscript-to-access-all-values-in-json-data) – user692942 Apr 29 '17 at 04:59
  • 1
    @JoelCoehoorn there are plenty of libraries for parsing JSON in Classic ASP. – user692942 Apr 29 '17 at 05:02
  • @JoelCoehoorn There are still tons legacy projects using Classic ASP. – Ravi Ram Apr 29 '17 at 10:52
  • 2
    @JoelCoehoorn - also, you can use Jscript rather than VBScript as your server side language in Classic ASP. JScript offers native handling of JSON – John Apr 29 '17 at 13:55

2 Answers2

5

Got it working:

Using https://github.com/rcdmk/aspJSON

Dim jsonString
jsonString = {"date":"4/28/2017","custType":"100","vehicle":"1"}

Dim jsonObj, outputObj
set jsonObj = new JSONobject
set outputObj = jsonObj.parse(jsonString)

response.write("<li> date :" & outputObj("date") & "</li>")
response.write("<li> custType :" & outputObj("custType") & "</li>")
response.write("<li> vehicle :" & outputObj("vehicle") & "</li>")

If you need to iterate through the single object use outputObj.pairs

Dim props, prop
props = outputObj.pairs
for each prop in props
    response.write prop.name & " : " & prop.value & "<br>"
next

as referenced https://github.com/rcdmk/aspJSON/issues/20

Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
2

As a quick solution for simple json structure and escaped values, this simple function returns the key values by splitting the json string on double quotes chr(34):

function getKeyValue(JsonString,key)
    myarray=split(JsonString,key,-1,1)
    if ubound(myarray)>0 then
        myarray2=split(myarray(1),chr(34),-1,1)
        getKeyValue=myarray2(2)
    else
        getKeyValue=""
    end if
end function

usage:

response.write("<li> date :" & getKeyValue(Your_Json_String_Here,"date") & "</li>")
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82