Arrays JSON/VBA中的键

Arrays JSON/VBA中的键,arrays,json,vba,excel,Arrays,Json,Vba,Excel,希望有人能给我一点提示来解决我的问题 我有一个Json响应,如下所示: "modules": [ { "localId": "598d58882e00008b1174fa0a", "legs": [ { "markerIndex": 0, "localId": "5a2ec9db250000cc0189fbac", "connection

希望有人能给我一点提示来解决我的问题

我有一个Json响应,如下所示:

"modules": [
    {
        "localId": "598d58882e00008b1174fa0a",
        "legs": [
            {
                "markerIndex": 0,
                "localId": "5a2ec9db250000cc0189fbac",
                "connections": [
                    {
                        "jsonClass": "TransitCO",
                        "localId": "5a882b0b26000039187fd0bb",
    {
        "localId": "598d58c82c00005411c4a7e1",
        "returnConnections": [
            {
                "jsonClass": "ActivityElementCO",
                "localId": "5a8aeacc250000641c1d389a",
    {
        "localId": "598d58d62e0000a71174fa0c",
        "legs": [
            {
                "markerIndex": 1,
                "localId": "5a85c668200000ea1b040503",
                "connections": [
                    {
                        "jsonClass": "TransitCO",
                        "localId": "5a882b0b26000039187fd0be",
我可以通过拨打以下电话找到1个本地ID:

Dim fd As Integer
Set var_dmc = JsonConverter.ParseJson(MyDMC.ResponseText)
Set dmc = Worksheets("dmc")
fd = 25
For Each item In var_dmc("modules")(1)("legs")
dmc.Cells(fd, 2) = item("connections")("localId")
fd = fd + 1
Next
现在,如果“jsonClass”是=TransitCO,那么我的VBA代码应该读取“connections”下的每个“localId”

尝试了每个for和“if-then”的组合,但没有任何效果

有什么想法吗

亲切问候,,
克里斯

这里有一个冗长的答案。JSON中有很多空结构(可能是编辑造成的)。但是我已经编写了代码来说明您仍然可以如何访问,尽管我已经注释掉了其中的许多部分。注释掉的
typename
语句将向您展示每个阶段返回的结构

诚然,这是目前的一切,所以我会期待一个较短的版本

注意:我正在从桌面上的文件中读取JSON

要更好地理解这一点,请参阅我的答案

更懒惰、更不健壮、更具针对性的版本:

Option Explicit

Sub GetvaluesDict()
    'Tools references > ms scripting runtime

    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject

    Dim JsonTS As TextStream
    Dim JsonText As String

    Set JsonTS = FSO.OpenTextFile(ThisWorkbook.Path & Application.PathSeparator & "SOQuestion.txt", ForReading)

    JsonText = JsonTS.ReadAll
    JsonTS.Close

    Dim Json As Object

    Set Json = ParseJson(JsonText)

    Dim col As Collection
    Set col = Json("modules")

    Dim counter As Long
    Dim dict As Dictionary

    Set dict = New Dictionary

    Dim item As Variant

    For Each item In col                         'looking at items

        Dim key1 As Variant

        For Each key1 In item.Keys

            If key1 = "returnConnections" Or key1 = "legs" Then '6 collections

                Dim item1 As Variant

                For Each item1 In item(key1)     ' 6 dictionaries

                    Dim key2 As Variant

                    For Each key2 In item1.Keys

                        Dim dataStructure As String

                        dataStructure = TypeName(item1(key2))

                        Select Case dataStructure

                        Case "Double", "String", "Boolean"
                            counter = counter + 1
                            dict.Add counter, key1 & " : " & key2 & " : " & item1(key2)

                        Case "Collection"

                            Dim item2 As Variant

                            For Each item2 In item1(key2)

                                Dim key3 As Variant

                                For Each key3 In item2.Keys

                                    Select Case TypeName(item2(key3))

                                    Case "String"

                                        counter = counter + 1
                                        dict.Add counter, key1 & " : " & key2 & " : " & key3 & " : " & item2(key3)

                                    End Select

                                Next key3

                            Next item2


                        End Select

                    Next key2

                Next item1

            End If

        Next key1

    Next item

    Dim returns As Variant

    counter = 1

    For Each returns In dict.Keys

        If InStr(1, dict(returns), "TransitCO", vbBinaryCompare) > 0 Then

            Debug.Print dict(returns) & vbTab & dict(counter + 1)

        End If
        counter = counter + 1
    Next returns

End Sub

您需要两个循环。一个用于变量dmc(“模块”)中的每个项目的
,另一个用于
项目(“腿”)
。类似于:Dim item1作为变量,用于变量dmc(“模块”)中的每个项目,用于MsgBox item1(“连接”)(“localId”)中的每个项目1(“腿”)下一个Next@QHarr在你的例子中,只有“legs”和“returnconnections”有“jsonClass”:“TransitCO”。那么,你想要的是连接下的“localId”吗?可能还有更多的JSON吗?我已经向你展示了如何打印所有值。
Option Explicit

Sub GetvaluesDict()
    'Tools references > ms scripting runtime

    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject

    Dim JsonTS As TextStream
    Dim JsonText As String

    Set JsonTS = FSO.OpenTextFile(ThisWorkbook.Path & Application.PathSeparator & "SOQuestion.txt", ForReading)

    JsonText = JsonTS.ReadAll
    JsonTS.Close

    Dim Json As Object

    Set Json = ParseJson(JsonText)

    Dim col As Collection
    Set col = Json("modules")

    Dim counter As Long
    Dim dict As Dictionary

    Set dict = New Dictionary

    Dim item As Variant

    For Each item In col                         'looking at items

        Dim key1 As Variant

        For Each key1 In item.Keys

            If key1 = "returnConnections" Or key1 = "legs" Then '6 collections

                Dim item1 As Variant

                For Each item1 In item(key1)     ' 6 dictionaries

                    Dim key2 As Variant

                    For Each key2 In item1.Keys

                        Dim dataStructure As String

                        dataStructure = TypeName(item1(key2))

                        Select Case dataStructure

                        Case "Double", "String", "Boolean"
                            counter = counter + 1
                            dict.Add counter, key1 & " : " & key2 & " : " & item1(key2)

                        Case "Collection"

                            Dim item2 As Variant

                            For Each item2 In item1(key2)

                                Dim key3 As Variant

                                For Each key3 In item2.Keys

                                    Select Case TypeName(item2(key3))

                                    Case "String"

                                        counter = counter + 1
                                        dict.Add counter, key1 & " : " & key2 & " : " & key3 & " : " & item2(key3)

                                    End Select

                                Next key3

                            Next item2


                        End Select

                    Next key2

                Next item1

            End If

        Next key1

    Next item

    Dim returns As Variant

    counter = 1

    For Each returns In dict.Keys

        If InStr(1, dict(returns), "TransitCO", vbBinaryCompare) > 0 Then

            Debug.Print dict(returns) & vbTab & dict(counter + 1)

        End If
        counter = counter + 1
    Next returns

End Sub