JSON files

User avatar
jornmo
Site Admin
Posts: 120
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post by jornmo » Friday 15th September 2017 6:08pm

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: 101
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: JSON files

Post by cogier » Saturday 16th September 2017 11:32am

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: 120
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post by jornmo » Saturday 16th September 2017 4:41pm

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: 2
Joined: Wednesday 21st September 2016 7:59pm
Location: Guernsey, Channel Islands

Re: JSON files

Post by Matthew-Collins » Sunday 17th September 2017 7:56am

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: 2
Joined: Wednesday 21st September 2016 7:59pm
Location: Guernsey, Channel Islands

Re: JSON files

Post by Matthew-Collins » Sunday 17th September 2017 8:12am

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: 120
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post by jornmo » Sunday 17th September 2017 9:31am

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: 120
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: JSON files

Post by jornmo » Sunday 17th September 2017 9:32am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests