Re: JSON files
Posted: Friday 15th September 2017 6:08pm
Try replacing:I can't get much from the Jornmo example. I tried the following
If v Is Collection Then
With:If TypeOf(v) = gb.Object Then
Try replacing:I can't get much from the Jornmo example. I tried the following
If v Is Collection Then
With:If TypeOf(v) = gb.Object Then
Public Sub Main()
Dim vNew, vTemp As Variant
Dim sNew As String
Dim cCol, cCol1 As Collection
Shell "wget -O - https://poloniex.com/public?command=returnTicker" To sNew
vNew = Json.decode(sNew) ''Requires component 'gb.web'
cCol = vNew
For Each cCol1 In cCol
Print cCol.Key
Next
For Each vTemp In cCol1
Print cCol1.Key
Next
End
ResultI suppose that's because your JSON file does not produce collections inside a collection, as with stevedee?!Your code works but the 'TypeOf (v)' is never 'True' so is not called.
Public Sub Main()
Dim vNew As Variant
Dim sNew As String
Shell "wget -O - https://poloniex.com/public?command=returnTicker" To sNew
vNew = Json.decode(sNew) 'Requires component 'gb.web'
GetCollectionData(vNew)
End
Public Sub GetCollectionData(Col As Collection)
Dim v As Variant
For Each v In Col
If TypeOf(v) = gb.Object Then
Print "Collection: Key: " & Col.Key
GetCollectionData(v)
Else
Print "Key: " & Col.Key & " Value: " & v
End If
Next
End
Results:Public Sub Main()
Dim vNew As Variant
Dim sNew As String
Dim Keys1 As String[]
Dim Keys2 As String[]
Shell "wget -O - https://poloniex.com/public?command=returnTicker" To sNew
vNew = Json.decode(sNew) 'Requires component 'gb.web'
Keys1 = GetKeys(vNew)
Keys2 = GetKeys(vNew[Keys1[0]])
Print Keys1[0]
Print Keys2[0]
End
Public Sub GetKeys(Col As Collection) As String[]
Dim Keys As New String[]
Dim v As Variant
For Each v In Col
Keys.Add(Col.Key)
Next
Return Keys
End
CheersAgreeI think the "Collection" could do with a ".Keys" as well as ".Key" function.... ?
Code: Select all
' Gambas class file
Public Sub Form_Open()
Dim myhttp As HttpClient
Dim cadena_respuesta As String
Dim cadena_peticion As String
Dim vNew As Variant
Dim sNew As String
Dim Keys1 As String[]
Dim Keys2 As String[]
myhttp = New HttpClient
myhttp.Async = False
myhttp.Timeout = 60
myhttp.URL = "https://poloniex.com/public?command=returnTicker"
myhttp.Get
cadena_respuesta = ""
JSON.Decode(cadena_respuesta)
sNew = cadena_respuesta
Print cadena_respuesta
vNew = Json.decode(sNew) 'Requires component 'gb.web'
Keys1 = GetKeys(vNew)
Keys2 = GetKeys(vNew[Keys1[0]])
Print Keys1[0]
Print Keys1[1]
Print Keys2[0]
End
Public Sub GetKeys(Col As Collection) As String[]
Dim Keys As New String[]
Dim v As Variant
For Each v In Col
Keys.Add(Col.Key)
Next
Return Keys
End