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.
2
Answers
For now, a less-than-optimal is to write a helper function, in JS itself, to get the key. For instance, I added this section to my local copy of json2.js file:
So that I can do:
So in the end I needed a JavaScript wrapper to get access to these keys. I'd gladly mark up as answer if somebody suggests a means to elegantly access these keys without wrapping thru javascript.
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?
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’serror-codes