-
Notifications
You must be signed in to change notification settings - Fork 575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to get data for parsed JSON object #202
Comments
The reason you cannot get the data for the parsed JSON object is because you don't understand how VBA-JSON maps a JSON
Your
A more robust coding of your sample code would be: Sub SendPOSTRequest_AndPrintOut()
Dim replyTXT As String
Dim JSON As Object ' Depending upon what the API returns you might need to use Variant instead.
Dim API as Scripting.Dictionary
Dim avgzone as Scripting.Dictionary
replyTXT = "{""avgzone"": {""green_end"": 1491.5602768288572, ""green_start"": 1744.3302566228513, ""red_end"": 199.04817530731853, ""red_start"": 304.2799829937485, ""yellow_end"": 304.2799829937485, ""yellow_start"": 1491.5602768288572}, ""skuzone"": [{""index"": 0, ""matid"": ""844595465000"", ""zone"": {""green_end"": 1511.2477987044917, ""green_start"": 1761.4327906976744, ""red_end"": 199.04817530731856, ""red_start"": 304.27998299374843, ""yellow_end"": 304.27998299374843, ""yellow_start"": 1511.2477987044917}}, {""index"": 1, ""matid"": ""849910852500"", ""zone"": {""green_end"": 526.8717049227604, ""green_start"": 906.3060869565215, ""red_end"": 199.04817530731856, ""red_start"": 304.27998299374843, ""yellow_end"": 304.27998299374843, ""yellow_start"": 526.8717049227604}}]}"
Set JSON = JsonConverter.ParseJson(replyTXT)
' First let's make sure that the Json parsed was a Json Object.
' Note, depending on the API being used checking for a successful message could be much more
' complicated since the API could return a JSON Object for success and a JSON Array, Object,
' String, Boolean, Number, Null for an error.
'
If TypeOf JSON Is Scripting.Dictionary _
Then
' Reference for the top level JSON Object of the message.
' Strictly, you don't need to do this, but it does gives you intellisense.
'
Set API = JSON
' When using the Scripting.Dictionary object you must test for the presence of the key
' **before** using the key, otherwise the Scripting.Dictionary object will **add** the key if it
' is not in the dictionary.
' See Remarks section: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/item-property-dictionary-object
'
If API.Exists("avgzone") _
Then
' In VBA we cannot make the following conditional part of the enclosing If statement's
' conditional because VBA does not perform short circuit evaluation of conditionals.
' See Wikipedia article: https://en.wikipedia.org/wiki/Short-circuit_evaluation
'
If TypeOf API.item("avgzone") Is Scripting.Dictionary _
Then
Set avgzone = API.item("avgzone")
' The rest of your code to do whatever you need to do.
Else
Debug.Print "API returned a JSON Object with an avgzone key whose value was not a JSON Object"
End If
Else
Debug.Print "API returned a JSON Object without an avgzone key"
End If
Else
Debug.Print "API did not return a JSON Object"
End If
Exit Sub
End Sub |
@houghtonap Sorry for did not close the issue, after reading your guide, I can access any values in the JSON object. Thanks for making this package for VBA |
I can not access the value of
avgzone
key from the sample of code below. could you please suggest?The text was updated successfully, but these errors were encountered: