-
Notifications
You must be signed in to change notification settings - Fork 91
Array parsing
Ricardo Souza edited this page Oct 3, 2022
·
2 revisions
A common scenario in JSON data is the use of array structures like
[{"name":"item 1"},{"name":"item 2"}]
or array properties like
{"list":[{"name":"item 1"},{"name":"item 2"}]}
Since we represent arrays with jsonArray
objects, we have to use Set
to obtain a reference for the array structure when parsing a JSON array string like the first example:
<%
Set json = new jsonObject
jsonStr = "[{""name"":""item 1""},{""name"":""item 2""}]" ' VBS escapes double quotes with double quotes
Set arr = json.Parse(jsonStr)
For item in arr.items
Response.Write item.("name") & "<br>"
Next
%>
For array properties, we can use the same strategy as above, setting the array reference to another variable and looping its items or access the items
property directly in the loop:
<%
Set json = new jsonObject
jsonStr = "{""list"":[{""name"":""item 1""},{""name"":""item 2""}]}" ' VBS escapes double quotes with double quotes
json.Parse(jsonStr) ' we don't need to set objects to new variables if the string is not an array
' setting to another variable
Set arr = json("list")
For item in arr.items
Response.Write item.("name") & "<br>"
Next
' OR, accessing the variable directly
For item in json("list").items
Response.Write item.("name") & "<br>"
Next
%>