TheRealPapa
TheRealPapa

Reputation: 4539

Check if Array at position contains another array

Please look at the code below. I am reading JSON using this code which works great, but, I am now trying to dump the resulting array into columns / rows. Some of my returned JSON values, contain arrays in them (example below "cat_list").

I am not able to understand why in the code below:

MsgBox "ITEM VALUE... " & p("data")(Row)(Col_Data)(1) 

shows the value '20' (which is correct for the JSON input below), but the code snippet:

If IsArray(p("data")(Row)(Col_Data)) Then  

resolves to FALSE, which then causes the ELSE part of the code to fail. Why is IsArray not working?

My JSON input looks like this:

{... "layout_file":"category.html","cat_list":[20, 30, 25], ...}

The code snippet:

Row = 1

For Each Item In p("data")
    Col = 1

    For Each Col_Data In p("data")(Row)

        If Col_Data = "cat_list" Then
            MsgBox "ITEM VALUE... " & p("data")(Row)(Col_Data)(1)   <-- SUCCESSFULLY PRINTS SUB-ARRAY VALUE "20"
        End If

        If IsArray(p("data")(Row)(Col_Data)) Then   <-- FAILS TO DETECT SUB-ARRAY
            Cells(Row + 1, Col) = "["

            For Each SubArrayData In p("data")(Row)(Col_Data)(SubArray)
                Cells(Row + 1, Col) = Cells(Row + 1, Col) & ", " & p("data")(Row)(Col_Data)(SubArray)
            Next SubArrayData

            Cells(Row + 1, Col) = Cells(Row + 1, Col) & "]"
        Else
            Cells(Row + 1, Col) = p("data")(Row)(Col_Data)   <-- CODE FAILS HERE ONLY WHEN ITEM CONTAINS ARRAY, BUT SUCCESSFULLY PRINTS VALUE "20" IN MSG BOX ABOVE
        End If

        Col = Col + 1
    Next Col_Data

    Row = Row + 1
Next Item

Thanks!

Upvotes: 2

Views: 103

Answers (1)

Santosh
Santosh

Reputation: 12353

Use the IsArray() function to check if a variable is an array.

enter image description here

sample code

Sub testArray()

Dim i() As Integer
Dim j As Integer


MsgBox IsArray(i)
MsgBox IsArray(j)

End Sub

Upvotes: 2

Related Questions