使用VBA JSON解析器解析JSON

时间:2018-01-15 19:46:36

标签: json vba parsing

我查看了堆栈溢出和vba JSON解析器的示例:https://github.com/VBA-tools/VBA-JSON。我无法弄清楚如何解析这个复杂的JSON:

;

我的目标是将事件参与者详细信息放入1个数据库表,将站点数据放入自己的表中。我跟着GitHub中的例子,但只能进入“芝加哥公牛队 - 奥兰多魔术队”。我哪里错了?

{
    "au": {
        "success": true,
        "data": {
            "events": {
                "Chicago Bulls_Orlando Magic": {
                    "participants": ["Chicago Bulls",
                    "Orlando Magic"],
                    "commence": "1489017900",
                    "status": "Pending",
                    "sites": {
                        "sportsbet": {
                            "odds": {
                                "h2h": ["1.86",
                                "1.99"]
                            },
                            "last_update": 1488956952
                        },
                        "tab": {
                            "odds": {
                                "h2h": ["1.70",
                                "2.10"]
                            },
                            "last_update": 1488957101
                        },
                        "crownbet": {
                            "odds": {
                                "h2h": ["1.83",
                                "1.98"]
                            },
                            "last_update": 1488957104
                        },
                        "williamhill": {
                            "odds": {
                                "h2h": ["1.83",
                                "2.00"]
                            },
                            "last_update": 1488957115
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

示例代码:

Sub Tester()
    Dim j, o, events, k, participants, v, sites

    'loading json from worksheet cell...
    Set j = JsonConverter.ParseJson(Sheet1.Range("A6").Value)

    Set events = j("au")("data")("events")

    For Each k In events
        Debug.Print "event", k

        Set participants = events(k)("participants")
        For Each v In participants
            Debug.Print , "participant", v
        Next v

        Set sites = events(k)("sites")
        For Each v In sites
            Debug.Print , "site", v
        Next v

    Next

End Sub

输出:

event         Chicago Bulls_Orlando Magic
              participant   Chicago Bulls
              participant   Orlando Magic
              site          sportsbet
              site          tab
              site          crownbet
              site          williamhill