I have this working, but it’s sytactically bad code and I should really ask for a better way. I’m struggling with accessing the Dictionary element of the Data() array, without using a “For Each” statement to iterate over the Data() variable.
Is there any way to directly access the Dictionary nested inside Data()?
In the code snippet below I’m using a URLConnection to hit an API and getting back the JSON response that looks like this:
// [{“slot”:0,“io”:{“ai”:[{“aiIndex”:0,“aiMode”:2, …many more name-value pairs… ,“aiBurnoutValue”:2},{“aiIndex”:1,“aiMode”:2, …and so on…
What I really want to access is the collection of “ai” values. The following code works, but it seems overly complex, and iterating over an array of (1) element seems wasteful and misleading. Thanks for any suggestions.
Var Data() As Variant
Data = ParseJSON(json_string_from_api_here)
Var readings as Dictionary
For Each d As Dictionary In Data
'System.DebugLog(d.Key(0).StringValue) ' "slot"
'System.DebugLog(d.Key(1).StringValue) ' "io"
Var searchString As String = d.Key(1).StringValue
If searchString = "io" Then
readings = d.Value("io")
End If
Next
Var io() As Variant
io = readings.Value("ai")
For Each reading As Dictionary in io
Var aiValueScaled As Double = Double.FromString(reading.Value("aiValueScaled"))
// "aiIndex":7,
// "aiMode":2,
// "aiValueRaw":13,
// "aiValueScaled":0.008363999999999999907,
// "aiValueRawMin":0,
// "aiValueRawMax":62,
// "aiValueScaledMin":0,
// "aiValueScaledMax":0.039889000000000000845,
// "aiResetMinValue":0,
// "aiResetMaxValue":0,
// "aiStatus":0,
// "aiBurnoutValue":2
Next