2

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!

2
  • Which line gives the Object Defined Error error ? Is it possible for you to share the workbook ? Commented Oct 10, 2013 at 4:56
  • Hi @Santosh the line giving the error is "Cells(Row + 1, Col) = p("data")(Row)(Col_Data)" in the ELSE statement from the IsArray comparion (see above). This is happening because "Cells(Row + 1, Col) = p("data")(Row)(Col_Data)" is an array but IsArray is not picking it up in the code above. Commented Oct 10, 2013 at 5:26

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Santosh, thanks for the reply, but I am still getting the same error. The message is OBJECT DEFINED error. I am expecting an array in the position, see above pls. Thanks!
@user2469528 If IsArray(p("data")(Row)(Col_Data)) Then ....do something....End If Try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.