I'm using ASPJSON (https://www.aspjson.com/) to write JSON in Classic ASP / VBScript. I'm then attempting to loop through the JSON and access both the key and value. As it stands I can only access the value.
JSON
Set InitialGlaDataJsonForm = New aspJSON
With InitialGlaDataJsonForm.data
.Add "glaFormContent", InitialGlaDataJsonForm.Collection()
With .item("glaFormContent")
.Add "form", "myform"
.Add "email", "email@address"
.Add "phone", "012345"
.Add "name", "Joe Bloggs"
End With
End With
JSON Looks like this
{
"glaFormContent": {
"form": "myform",
"email": "email@address",
"phone": "012345",
"name": "Joe Bloggs"
}
}
Looping through the JSON, accessing the value
For Each element In InitialGlaDataJsonForm.data("glaFormContent")
response.write InitialGlaDataJsonForm.data("glaFormContent").item(element) & "<br>"
Next
The above returns the value of during each iteration, however I need to be able to get hold of the key, without writing it out.
So my question is how do I access the key in the same manner as the value?
Many thanks
Response.Write elementwill give you the key. In ASPJSON when looping through thedatadictionary,elementis your key and.item(element)gives you the value related to that key.