解析JSON,MS Access VBA(嵌套循环)

时间:2018-03-26 15:31:06

标签: json vba dictionary collections access

StackOverflow的好人!

我想弄清楚如何连接到Airtable API,从那里获取JSON并用结果填充Access表。

到目前为止,至少我设法限制请求只给我一些我需要的字段。结果如下:

{
  "records": [{
    "id": "rec008lgyvVmwk1F4",
    "fields": {
      "Date": "2018-02-28"
    },
    "createdTime": "2018-01-26T15:36:23.000Z"
  }, {
    "id": "rec02WozJeaGvfBfj",
    "fields": {
      "Hours": 1.5,
      "Date": "2018-02-09",
      "Project": ["Nonbillable"]
    },
    "createdTime": "2018-02-12T17:03:18.000Z"
  }, {
    "id": "rec05VxP0CYTsDYOA",
    "fields": {
      "Date": "2018-02-08"
    },
    "createdTime": "2018-02-01T10:29:52.000Z"
  }, {
    "id": "rec05xoQEm5iWIYmz",
    "fields": {
      "Hours": 0.75,
      "Date": "2018-02-16",
      "Project": ["2018 - Japan DLAs"]
    },
    "createdTime": "2018-02-19T09:29:18.000Z"
  }]
}

从那时起,我已经阅读了很多例子,因为我可以找到如何使用Tim Hall的VBA-JSON(感谢Tim,创建它:)

据我所知,在我的例子中,ParseJson函数返回一个字典。里面是一个名为'results'的集合,在该集合中是另一个名为'fields'的字典。 我需要的是该词典的密钥'小时','日期'和'项目'的值。

我已经尝试过这三个循环(循环遍历字典内的集合内的字典),并且因各种错误多次失败。最后,我已经到了没有更多错误的地步,sub愉快地给了我“导入完成!”信息。唉,我的桌子是空的! 什么,我做错了什么?

我希望我能给你足够的信息,并提前感谢你的帮助!

(如果重要的话,我在64位Windows上使用32位Access 2016)

Public Sub ImportJSON()

    Dim reader As New XMLHTTP60
    Dim JsonRetrieved As String
    Dim Parsed As Scripting.Dictionary
    Dim records As New Collection
    Dim fields As Scripting.Dictionary
    Dim item As Variant
    Dim rs As New ADODB.Recordset


    reader.Open "GET", "https://api.airtable.com/v0/apppLTTgKBsw5QmUX/myTable?fields[]=Project&fields[]=Hours&NOT({Hours} = '')&fields[]=Date&NOT({Date} = '')&maxRecords=4&api_key=mykey", False

    reader.setRequestHeader "Accept", "application/json"

    reader.Send

    Do Until reader.ReadyState = 4
        DoEvents
    Loop

    If reader.Status = 200 Then

        rs.Open "tblAirtableImport", CurrentProject.Connection, _
         adOpenKeyset, adLockOptimistic

        JsonRetrieved = reader.responseText
        'Debug.Print JsonRetrieved

       Set Parsed = JsonConverter.ParseJson(JsonRetrieved)

        'loop through dictionary 'Parsed'
        Dim i As Long
        For i = 0 To Parsed.Count - 1

            'loop through collection 'records'. If we have hours logged into Airtable add new record (Hours, Date, Project) to Access table
            For Each fields In records

                'loop through dictionary 'fields'
                Dim j As Long
                For j = 0 To fields.Count - 1

                    If fields.Exists("Hours") Then
                    'MsgBox "We have hours in this row"


                        rs.AddNew

                        rs!AirtableHours = fields.item("Hours")
                        rs!AirtableDate = fields.item("Date")
                        rs!AirtableProject = fields.item("Project")

                        rs.Update
                    Else
                        MsgBox "No logged time."
                    End If

                Next j
            Next

        Next i

        MsgBox "Import done!"
        Set Parsed = Nothing

    Else
        MsgBox "Ups, unable to import data. Reader status is: " & reader.Status
    End If    

End Sub

1 个答案:

答案 0 :(得分:2)

条目 for(int i = 0; i < 4; i++) row[1].add(button[i]); row[1].add(button[14]); add(row[1]); for(int i = 4; i < 8; i++) row[2].add(button[i]); row[2].add(button[15]); add(row[2]); for(int i = 8; i < 12; i++) row[3].add(button[i]); row[3].add(button[16]); add(row[3]); row[4].add(button[18]); for(int i = 12; i < 14; i++) row[4].add(button[i]); row[4].add(button[17]); add(row[4]); 包含一个数组,所以取第一个项目:

Project