1

I have this code:

Dim list As Object
Set list = jsonParse.Item("list")

Dim groupedByProjectElement As New Dictionary
For i = 1 To 2
        Set pE = list(i).Item("projectElement")
        Dim projectElementsDictionary As New Dictionary
        If groupedByProjectElement.Exists(pE.Item("businessKey")) Then
            Set projectElementsDictionary = groupedByProjectElement(pE.Item("businessKey"))
            projectElementsDictionary.Add projectElementsDictionary.count + 1, list(i)
        End If
        If Not groupedByProjectElement.Exists(pE.Item("businessKey")) Then
            projectElementsDictionary.Add 1, list(i)
            groupedByProjectElement.Add pE.Item("businessKey"), projectElementsDictionary
        End If
Next i

In the second iteration I am getting the error:

This key is already associated with an element of this collection

But I am expecting that in the second iteration projectElementsDictionary should be a new Dictionary already.

What am I missing?

list looks something like this:

"list": [
    {
        "projectElement": {
            "businessKey": "123",
            "customerUnit": {
                "businessKey": "123",
                "links": [],
                "shortDescription": "123",
                "text": "123",
                "version": null
            }
        }  
    }
]
0

1 Answer 1

1

I will answer my own question:

Dim groupedByProjectElement As New Dictionary
For i = 1 To 25
    Set pE = List(i).Item("projectElement")
    Dim projectElementsDictionary As Dictionary
    If groupedByProjectElement.Exists(pE.Item("businessKey")) Then
        Set projectElementsDictionary = groupedByProjectElement(pE.Item("businessKey"))
        projectElementsDictionary.Add projectElementsDictionary.Count + 1, List(i)
    Else
        Set projectElementsDictionary = New Dictionary
        projectElementsDictionary.Add 1, List(i)
        groupedByProjectElement.Add pE.Item("businessKey"), projectElementsDictionary
    End If
Next i
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.