How to access data from Json array in Classic ASP

Viewed 567

My end goal is to create a dynamic menu from json. I have a problem with reaching the children array. I can print out text from top level hierarchy but nothing below that. My goal for now is to print all the text fields from this json to html.

I am not experienced with ASP classic and I've been trying to figure this out all day.

Here is a simplified version of the json.

 [
   {
      "text":"Menüü",
      "children":[
         {
            "text":"Helistamine"
         },
         {
            "text":"Info"
         },
         {
            "text":"Kontaktide nimekiri"
         }
      ]
   },
   {
      "text":"Admin",
      "children":[
         {
            "text":"Kõik kirjed",
            "children2":[
               {
                  "text":"Vaata"
               },
               {
                  "text":"Muuda"
               }
            ]
         }
      ]
   }
]

I'm using ASP JSON Class. I get error: Object not a collection

Here is the code which should theoretically print out all data in children:

    'Load JSON string'
oJSON.loadJSON(jsonString)

'Get single value'
Response.Write oJSON.data("text") & "<br>"

'Loop through collection'
For Each phonenr In oJSON.data("children")
    Set this = oJSON.data("children").item(phonenr)
    Response.Write _
    this.item("text") & "<br>"
Next

I tried the same code with json that isn't an array and it worked fine. If anyone could point me in the right direction I would highly appreciate it!

Edit:

I changed json to:

{
   "data":[
      {
         "text":"Menüü",
         "children":[
            {
               "text":"Helistamine"
            },
            {
               "text":"Info"
            },
            {
               "text":"Kontaktide nimekiri"
            }
         ]
      },
      {
         "text":"Admin",
         "children":[
            {
               "text":"Kõik kirjed",
               "children2":[
                  {
                     "text":"Vaata"
                  },
                  {
                     "text":"Muuda"
                  }
               ]
            }
         ]
      }
   ]
}

And I changed code to:

For Each x In oJSON.data("data")("children")
Set this = oJSON.data("data")("children").item(x)
        response.Write this.item("text") & "<br>"

Next

But I still get the same error Object is not a collection.

Edit(2):

For Each x In oJSON.data("data")
Set this = oJSON.data("data").item(x)
        response.Write this.item("text") & "<br>"
For Each y In x.data("children") 'need a replacement for x'
    Set this = oJSON.data("children").item(y)
        response.Write this.item("text") & "<br>"

Next
Next

Object required: '0 - I know x is wrong

Edit 3(Problem solved): Finally I can access the children part of json. (I changed "edit" to "parent" because "edit" is system variable)

     'Loop through collection'
        For Each x In oJSON.data("data")
set parent = oJSON.data("data").item(x)
        response.Write parent.item("text") & "<br>"
For Each y In parent("children")
    Set child = parent("children").item(y)
        Response.Write child.item("text") & "<br>"

Next

Next

Now prints as intended:

'Menüü
Helistamine
Info
Kontaktide nimekiri
Admin
Kõik kirjed'

Last question. I'm attempting to print out children of children by using the same logic. The code gives error object not a collection.

For Each x In oJSON.data("data")
    set parent = oJSON.data("data").item(x)
    response.Write parent.item("text") & "<br>"
For Each y In parent("children")
    Set child = parent("children").item(y)
    Response.Write child.item("text") & "<br>"
For Each z In child("children2") 
    Set grandchild = child("children2").item(z) 
    Response.Write grandchild.item("text") & "<br>" 

Next
Next
Next

Edit 4: Here is the solution to my latest problem

For Each x In oJSON.data("data")
    set parent = oJSON.data("data").item(x)
    response.Write parent.item("text") & " "

if isObject(parent("children")) then
    For Each y In parent("children")
        Set child = parent("children").item(y)
        Response.Write child.item("text") & " "

if isObject(child("children2")) then
For Each z In child("children2") 
    Set grandchild = child("children2").item(z) 
    Response.Write grandchild.item("text") & "<br>" 

Next
end if
Next
end if
Next
0 Answers
Related