使用JSON-VBA嵌套For循环

使用JSON-VBA嵌套For循环,json,vba,Json,Vba,所以基本上我在这里做的是双重搜索一些关于客户的信息。我可以在一次搜索中完成它,但是它将花费5倍于这里应该发生的时间 首先,我按客户电话号码搜索,并查看与他的电话号码链接的每个帐户的ID。然后我启动另一个For循环,通过另一个xmlhttprequest搜索每个帐户,看看他是否有一个活动帐户。totalCount用于查看用户有多少帐户以及第一个for循环请求中有多少嵌套数组。然后,该过程应在每个用户的帐户中不断循环,一旦找到第一个活动帐户,则整个过程应在相应的excel相邻单元格中返回1 但我收到

所以基本上我在这里做的是双重搜索一些关于客户的信息。我可以在一次搜索中完成它,但是它将花费5倍于这里应该发生的时间

首先,我按客户电话号码搜索,并查看与他的电话号码链接的每个帐户的ID。然后我启动另一个For循环,通过另一个xmlhttprequest搜索每个帐户,看看他是否有一个活动帐户。totalCount用于查看用户有多少帐户以及第一个for循环请求中有多少嵌套数组。然后,该过程应在每个用户的帐户中不断循环,一旦找到第一个活动帐户,则整个过程应在相应的excel相邻单元格中返回1

但我收到:

错误9,下标超出范围

在For循环体系结构中我应该纠正什么

编辑:这是第一个for循环的第一个JSON数组示例:

网址。http://controlpanel.zoho/rest/Accounts/criteria/idlist?AccountPhone=67545678

而JSON:

 {"totalCount":4,
  "messages":[],
   "results":[
        {"Type":"FX","AccountId":14237},
        {"Type":"FX","AccountId":17152},
        {"Type":"FX","AccountId":17553},
        {"Type":"FX","AccountId":17553}
    ],
   "resultClass":"com.zoho.dao.dto.zohoAccountMarketTypeDTO"}
这是第二个For循环中的第二个JSON示例:

URL 2,例如

您正在循环中使用http变量,因此在第一次传递时卸载原始响应。只需解析原始响应并将其保存在一个变量中,然后在项目上循环,并获取每个项目的详细信息

未经测试,但类似的方法应该有效:

Sub Tester()

    Dim theListing As Object, theDetails As Object
    Dim totalCount As Integer, count As Integer
    Dim userID As String
    Dim status As String, jsonDate As Integer

    For x = 1 To NumRows

        Set theListing = JSONObject("http://controlpanel.zoho/rest/Accounts/" & _
                                    "criteria/idlist?AccountPhone=" & var)

        totalCount = theListing("totalCount")

        For count = 1 To totalCount 'totalCount can be 1 sometimes

            userID = theListing("results")(count)("AccountId")


            Set theDetails = JSONObject("http://controlpanel.zoho/rest/" & _
                                        "AccountDetails/" & userID)

            status = theDetails("results")(1)("CustomerStatus")
            If status = "LIVE_ACTIVE" Then 'can introduce Date condition to see if peolpe activated after a certain date
                Sheet3.Cells(RowNote, 3).Value = 1
                Exit For
            End If
        Next count
    Next x

End Sub

'fetch a response as parsed JSON object
Function JSONObject(url As String)
    Dim http As Object
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", url, False
    http.send
    Set JSONObject = parsejson(http.responseText)
End Function

哪一行抛出错误?@CallumDA似乎在IF语句中rowNote的值是多少?还有,为什么它被声明为字符串?如果它是一个Long@CallumDA通过我的项目,我有许多其他模块可以正常使用rownote。这是我第一次使用嵌套循环,我通常使用rownote来迭代电子表格中的行。为什么你暗示问题来自rownote?然后你需要做一些事情,比如如果count>=totalCount然后退出ForHank you Tim以获得你的答案,关于count=count+1,它仍然在for循环中,尽管你通知我不应该修改它。或者是我误解了什么。我没有试图弄明白为什么会这样;只是指出这通常不是使用For循环变量所能做的事情。如果它能正常工作,但这不是一个好的练习…Tim,如果totalcount有时恰好为1,我会有迭代问题吗?此外,一旦条件为真,我是否应该添加:exit for,以便在第一个for循环中查找其他用户?是的,听起来exit for是您需要的
{"totalCount":1,
 "messages":[],
  "results":[
      {"AccountAgrt":false, 
       "accountType":"FOLLOWER",
       "CustomerId":9069,
       "logins":81,
       "CustomerStatus":"LIVE_ACTIVE",
       "dateLastLogin":1510153414000,
       "state":null}
    ],
   "resultClass":"com.zoho.dao.dto.zohoAccountInfoDTO"}
Sub Tester()

    Dim theListing As Object, theDetails As Object
    Dim totalCount As Integer, count As Integer
    Dim userID As String
    Dim status As String, jsonDate As Integer

    For x = 1 To NumRows

        Set theListing = JSONObject("http://controlpanel.zoho/rest/Accounts/" & _
                                    "criteria/idlist?AccountPhone=" & var)

        totalCount = theListing("totalCount")

        For count = 1 To totalCount 'totalCount can be 1 sometimes

            userID = theListing("results")(count)("AccountId")


            Set theDetails = JSONObject("http://controlpanel.zoho/rest/" & _
                                        "AccountDetails/" & userID)

            status = theDetails("results")(1)("CustomerStatus")
            If status = "LIVE_ACTIVE" Then 'can introduce Date condition to see if peolpe activated after a certain date
                Sheet3.Cells(RowNote, 3).Value = 1
                Exit For
            End If
        Next count
    Next x

End Sub

'fetch a response as parsed JSON object
Function JSONObject(url As String)
    Dim http As Object
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", url, False
    http.send
    Set JSONObject = parsejson(http.responseText)
End Function