3

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
Innit2
  • 107
  • 1
  • 7
  • The data you've quoted is an array, but you're accessing it as though you were only dealing with one *entry* in that array. Do you have some outer loop you haven't shown us? – T.J. Crowder May 28 '20 at 16:05
  • 1
    The problem is you are an array as the start of the JSON structure, it can't find `oJSON.data("children")` and so can't use it as a collection in a `For` loop. It's also an unnamed array which makes it difficult to access if you can give it a name something like `data: [ ... ]` then you'll be able to call it like `oJSON.data("data")` and access the collection. – user692942 May 28 '20 at 16:25
  • Thanks for the help but I still couldn't fix the issue. I edited the post to show the changes. – Innit2 May 28 '20 at 20:07
  • 1
    @Innit2 You need two `For` loops, one to iterate through `oJSON.data("data")` then through `x.data("children")`. – user692942 May 28 '20 at 20:13
  • Thanks for input. I tried making two for loops, tried all combinations I could think of. But I got the same error on the For loop line for Children. I also double checked if json is in valid format. – Innit2 May 28 '20 at 20:47
  • I would just like to get access to the "children" . Once I do, I can figure it out how to proerly create the markup for the table. – Innit2 May 28 '20 at 20:54
  • @Innit2 [edit] your latest attempt and i'll try to point you in the right direction. With each loop you need to `Set` the instance of that object inside the collection to then pass in the collection of that instance. What I mean is you can't do `oJSON.data("data")("children")`, it's a `Scripting.Dictionary` object behind the scenes it takes a string key and an object as the value it's not a traditional multi dimension array. – user692942 May 28 '20 at 20:58
  • Edited my post. I'm having trouble setting the right object. – Innit2 May 28 '20 at 21:18
  • 1
    @Innit2 so you are nearly there but `x` and `y` are the keys not object instances *(remember it's a `Scripting.Dictionary` is the collection, which uses string keys and object values)*. You need to use the key to instantiate the object instance. Do not use `this` twice as a variable, you need two separate instances we will call `item` and `child`. Change the first `Set` in the first loop to `Set item = oJSON.data("data").item(x)` and then change the inner `For` loop to `For Each y In item.data("children")` and the second `Set` to `Set child = item.data("children").item(y)`. – user692942 May 28 '20 at 23:29
  • 1
    @Innit2 you should then be able to access the properties of the `child` object. i.e. `Response.Write child.item("text") & "
    "`.
    – user692942 May 28 '20 at 23:33
  • 1
    I had to make few small changes and then It worked. Thank you very much! I really appreciate your help. How can I mark your answer as solution? – Innit2 May 29 '20 at 06:48
  • Technically this is a duplicate of another question, obviously the specifics are a bit different but as such your free to leave on upvote on that answer if you feel like this helped. – user692942 May 29 '20 at 07:32
  • 1
    I edited my question(Hopefully for the last time haha). Side question, If I would like to add another level so Children could also have children then I should just make one more for loop like this?: For Each z In child("children") Set grandchild = child("children").item(z) Response.Write grandchild.item("text") & "
    "
    – Innit2 May 29 '20 at 09:09
  • 1
    @Innit2 spot on, think you've got it sussed now. – user692942 May 29 '20 at 09:32
  • @Lankymart I thought so too but for some reason it gives the same error if I try to reach "children2' – Innit2 May 29 '20 at 10:14
  • 1
    @Innit2 need to see the changed JSON structure to understand what you might be causing the error. – user692942 May 29 '20 at 12:08
  • @LankyMart The structure of JSON is the same. If you look at the first json example I posted. Look where is( text: admin, children: children2). I am trying to access children2. I hope I am being clear enough :) You tell me if I'm not – Innit2 May 29 '20 at 12:47
  • 1
    @Innit2 try adding a `children2` into the first `children` element, I think because the objects are different (only one has `children2`) is what might be causing the error. – user692942 May 29 '20 at 14:54
  • @innit2 Thank you very very much, you saved a lot of time! I edited my original post to show how I fixed it. – Innit2 May 29 '20 at 20:24

0 Answers0