请查看下面的代码。我正在使用this code阅读JSON,这很好用,但是,我现在正在尝试将生成的数组转储到列/行中。我返回的一些JSON值包含数组(例如“cat_list”下面的例子)。
我无法理解下面代码中的原因:
MsgBox "ITEM VALUE... " & p("data")(Row)(Col_Data)(1)
显示值'20'(这对于下面的JSON输入是正确的),但是代码片段:
If IsArray(p("data")(Row)(Col_Data)) Then
解析为FALSE,然后导致代码的ELSE部分失败。为什么IsArray无法正常工作?
我的JSON输入如下所示:
{... "layout_file":"category.html","cat_list":[20, 30, 25], ...}
代码段:
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
谢谢!
答案 0 :(得分:2)
使用IsArray()
函数检查变量是否为数组。
示例代码
Sub testArray()
Dim i() As Integer
Dim j As Integer
MsgBox IsArray(i)
MsgBox IsArray(j)
End Sub