Page 2 of 3

Re: JSON files

Posted: Friday 15th September 2017 6:08pm
by jornmo
I can't get much from the Jornmo example. I tried the following
Try replacing:
If v Is Collection Then
With:
If TypeOf(v) = gb.Object Then

Re: JSON files

Posted: Saturday 16th September 2017 11:32am
by cogier
Your code works but the 'TypeOf (v)' is never 'True' so is not called.

This code will get the 'Keys' of the 1st and 2nd depth.
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 
Result
.....
BTC_OMG
ETH_OMG
BTC_GAS
ETH_GAS
id
last
lowestAsk
highestBid
percentChange
baseVolume
quoteVolume
isFrozen
high24hr
low24hr

Re: JSON files

Posted: Saturday 16th September 2017 4:41pm
by jornmo
Your code works but the 'TypeOf (v)' is never 'True' so is not called.
I suppose that's because your JSON file does not produce collections inside a collection, as with stevedee?!

Re: JSON files

Posted: Sunday 17th September 2017 7:56am
by Matthew-Collins
Hi Guys,

TypeOf(v) is true, if you pass the whole result from Json.decode... see below
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:
Collection: Key: BTC_BCN
Key: id Value: 7
Key: last Value: 0.00000034
Key: lowestAsk Value: 0.00000035
Key: highestBid Value: 0.00000034
Key: percentChange Value: -0.02857142
Key: baseVolume Value: 39.58161827
Key: quoteVolume Value: 115639167.85117029
Key: isFrozen Value: 0
Key: high24hr Value: 0.00000036
Key: low24hr Value: 0.00000033
Collection: Key: BTC_BELA
Key: id Value: 8
Key: last Value: 0.00003115
...

Re: JSON files

Posted: Sunday 17th September 2017 8:12am
by Matthew-Collins
Hi Guys,

I think the "Collection" could do with a ".Keys" as well as ".Key" function.... ?

In the meantime, see my GetKeys(Collection) function below:
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
Cheers
Matt

Re: JSON files

Posted: Sunday 17th September 2017 9:31am
by jornmo
If you tick "Do not automatically parse URLs" below the posting form, the URL's will display properly inside the code highlighter. (I will sooner or later need to make the plugin skip parsing automatically)

Re: JSON files

Posted: Sunday 17th September 2017 9:32am
by jornmo
I think the "Collection" could do with a ".Keys" as well as ".Key" function.... ?
Agree 8-)

Re: JSON files

Posted: Monday 28th March 2022 4:29pm
by karthiksg
i am getting unknown identifier Json error
anyone can help ?

Image

Re: JSON files

Posted: Tuesday 29th March 2022 2:55pm
by cogier
Welcome to the forum.

Try going to Project > Properties... > Components and ensure you have the gb.util.web component loaded.

Image

Re: JSON files

Posted: Friday 21st October 2022 6:10pm
by DayanUt31
Hello! New to Gambas here. I was trying to do an example using httpClient instead of Wget but no matter what i always end getting Null when i try to JsonDecode.
Does anyone know what i may be doing wrong here? Any help is appreciated.

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