You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sub TestJson()
Dim JSON AsScripting.Dictionary
Dim vItem AsScripting.Dictionary
Dim vResponse AsStringDim vArray AsVBA.CollectionDim vDict AsScripting.Dictionary
Dim vChild AsScripting.Dictionary
'Response from Http Request
vResponse = "{'Name':'Report001','Fields':[{'Name':'pCompetInicial'},{'Name':'pCompetFin'},{'Name':'pGrupoProcessamento'},{'Name':'pCodigoEmpresa'},{'Name':'pQuebra','Options':[{'Option XPTO':0},{'Option X':1},{'Option FOO':2},{'Option BAR':3}]},{'Name':'pOrdenar','Options':[{'Some Informarmation':0},{'Another Informarmation':1},{'Test':2}]},{'Name':'pCodigoIdioma'}],'Version':'23.5.0.0'}"
vResponse = Replace(vResponse, "'", """")
Set JSON = JsonConverter.ParseJson(vResponse)
'Access single node
Debug.Print JSON("Version")
'Access a node with childrenFor Each vItem In JSON("Fields")
If vItem.Exists("Options") ThenIfTypeOf vItem("Options") Is VBA.CollectionThen'How do I Access "Options" (all indexes and values) (by Key or Value or index)End If
Debug.Print vItem("Name") & " - Exist Options"Else
Debug.Print vItem("Name")
End IfNext vItem
End Sub
Thanks
The text was updated successfully, but these errors were encountered:
You process Options in the same way that you process Fields.
Each option is (should be) a Dictionary. You get the list of keys with the Keys method.
Change the loop contents to ...
If vItem.Exists("Options") Then
Debug.Print vItem("Name") & " - Exist Options"
ProcessOptions vItem("Options")
Else
Debug.Print vItem("Name")
End If
and then add ...
Sub ProcessOptions(vOptions As Variant)
Dim vDict As Scripting.Dictionary
Dim vKey As Variant
If Not TypeOf vOptions Is VBA.Collection Then
Debug.Print "*** Options not a collection = "; TypeName(vOptions)
Exit Sub
End If
For Each vDict In vOptions
For Each vKey In vDict.Keys
Debug.Print "", vKey, vDict(vKey)
Next vKey
Next vDict
End Sub
Hello Guys
How can I get data from Json "Name" and "Options"?
How Option keys are always different, I would like to access using maybe the index?
I've read some another issues, but I didn't understand very well.
It would be interesting to include more examples in github, this could clarify how to use and avoid new issues.
My Json
Thanks
The text was updated successfully, but these errors were encountered: