简体   繁体   中英

Parse Json string to Classic ASP page

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>")

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

As a funny solution as the JSON keys are wrapped by double quote, this simple function can extract the key values (however it may need some more details to scape quotations within the values):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM