JSON files

Post your Gambas programming questions here.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post 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
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: JSON files

Post 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
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post 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?!
User avatar
Matthew-Collins
Posts: 12
Joined: Wednesday 21st September 2016 7:59pm
Location: Guernsey, Channel Islands

Re: JSON files

Post 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
...
Cheers
Matt
User avatar
Matthew-Collins
Posts: 12
Joined: Wednesday 21st September 2016 7:59pm
Location: Guernsey, Channel Islands

Re: JSON files

Post 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
Cheers
Matt
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post 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)
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post by jornmo »

I think the "Collection" could do with a ".Keys" as well as ".Key" function.... ?
Agree 8-)
karthiksg
Posts: 3
Joined: Monday 28th March 2022 2:55pm

Re: JSON files

Post by karthiksg »

i am getting unknown identifier Json error
anyone can help ?

Image
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: JSON files

Post by cogier »

Welcome to the forum.

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

Image
DayanUt31
Posts: 3
Joined: Friday 21st October 2022 6:03pm

Re: JSON files

Post 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
Post Reply