jsonasp-classicjscriptjsonparser

Classic ASP and json2.js: how to access keys with special characters?


In this answer: https://stackoverflow.com/a/1021848/5280471 it is suggested to use a "language-wrapping" technique to implement JSON parsing by importing a json2.js JavaScript file like so:

<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
%>

Where json2.js can be found at https://github.com/douglascrockford/JSON-js/blob/master/json2.js .

The problem rose when I was trying to interpret a google recaptcha return JSON, which is:

{"success":false,"error-codes":["invalid-input-response"]}

So, I can access myJSON.success no problem. Yet I don't know how I could access, from ASP, the error-codes array. Is there any way that could be achieved from ASP, or am I stuck there?

I already tried some random tricks, like calling MyJSON.get("error-codes"), some obvious syntax errors like MyJSON("error-codes"), MyJSON."error-codes" and also hoped for the best, MyJSON.error_codes as well as MyJSON.["error-codes"]. All of them returning their respective exception, code-breaking errors.


Solution

  • I am no longer in an environment where I can test this myself, but if memory servers, classic asp allowed special chars as property names, when they were enclosed in brackets.

    Have you tried the following syntax?

    MyJSON.[error-codes]
    

    You said that you have tried MyJSON.["error-codes"], but since everything inside the brackets is taken literally, the interpreter would look for a property named "error-codes", but there isn't - it's error-codes