VBA-tools / VBA-Dictionary

Drop-in replacement for Scripting.Dictionary on Mac
MIT License
358 stars 89 forks source link

How to navigate dictionary #9

Closed jbhadlo closed 9 years ago

jbhadlo commented 9 years ago

First of all let me thank you for your tool. It has helped me quite a bit to request and receive JSON. However I am having an issue that I hope you can help with. I read in another blog of yours that you currently due not have any code to parse JSON. However you have neatly put the JSON response in a dictionary that is embedded in a collection within an object. Specifically the structure looks like this. Response - Data - Item 1 - pDictionary - Item 1. What I cannot figure out is the required code to get to the value of the Keys within the pDictionary. Also not sure how I will iterate through the Items within Data to them grab the key values within pDictionary. If you can help me with some iteration logic or the code needed to view the key values it would be great. Thanks again.

timhall commented 9 years ago

Hi @jbhadlo I use VBA-JSON for parsing JSON strings into Dictionary / Collection (it's XML that's currently being developed). Here are some examples for working with Dictionary:

Dim Results As New Dictionary
Results.Add "a", 123
Results.Add "b", 456
Results.Add "c", 789

Results.Items ' -> 123, 456, 789
Results.Keys ' -> "a", "b", "c"

' For iterating, Item/Key can be Variant/Object/Class
' (can't be low-level type: String, Double, Boolean, etc.)
Dim Result As Variant
Dim Key As Variant

' Iterate through Items
For Each Result In Results.Items
  Debug.Print Result
Next Result
' -> 123, 456, 789

' Iterate through Keys
For Each Key In Results.Keys
  Debug.Print Key & " = " & Results(Key)
Next Key
' -> a = 123, b = 456, c = 789
jbhadlo commented 9 years ago

Tim thank you for the quick response. What you sent makes sense if you dealing directly with a dictionary but what I have is embedded. What if the dictionary is embedded within the WebResponse Object then within each Item of the Data ObjectCollection. Once I navigate to each item within the Data Collection I will find the dictionary containing the elements for that one item in the Data Object/Collection. I have attached an image to show you what I am talking about.

image

timhall commented 9 years ago

In that case you can use the following:

Dim Item As Dictionary
Dim Key As Variant

' First, iterate through collection stored in Response.Data
For Each Item In Response.Data
  ' Then iterate through Dictionary key-values
  For Each Key In Item.Keys
    Debug.Print Key & " = " & Item.Items(Key)
  Next Key
Next Item

' If you need to know item index as well
' (Collections are 1-based)
Dim i As Long
For i = 1 To Response.Data.Count
  Set Item = Response.Data(i)

  For Each Key In Item.Keys
    Debug.Print "Item " & i & ": " & Key & " = " & Item.Items(Key)
  Next Key
Next i
jbhadlo commented 9 years ago

Thank you Tim. I'll give this a go and let you know.

jbhadlo commented 9 years ago

Tim, thanks for the help. With some slight modifications I am able to make this work for my specific needs.