JSON files

Post your Gambas programming questions here.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: JSON files

Post by cogier »

Hi DayanUt31 and welcome to the forum.

I suggest you start a 'New Topic' next time you have a question.
Tip: - When adding Gambas code to your posts use the 'gb' button. The code looks a lot better, see below
Image

Regarding your query, put the code below in a Graphical Application and run it, hopefully it will answer some of your questions.
' Gambas class file

''*******************************
''REQUIRES gb.net.curl and gb.web
''*******************************

GridViewData As GridView

Public Sub Form_Open()

  BuildForm
  GetData

End

Public Sub GetData()

  Dim myhttp As HttpClient
  Dim sResult, sVal As String
  Dim vResult As Variant
  Dim cCol As Collection
  Dim iRow As Integer

  myhttp = New HttpClient
  myhttp.Async = False
  myhttp.Timeout = 60
  myhttp.URL = "https://poloniex.com/public?command=returnTicker"
  myhttp.Get

  If Lof(myhttp) Then sResult = Read #myhttp, Lof(myhttp)

  vResult = JSON.Decode(sResult)

  For Each cCol In vResult
    For Each sVal In cCol
      Inc GridViewData.Rows.Count
      GridViewData[iRow, 0].Text = cCol.Key
      GridViewData[iRow, 1].Text = sVal
      Inc iRow
    Next
    Inc GridViewData.Rows.Count
    Inc iRow
  Next

  GridViewData.Columns.Width = -1

End

Public Sub BuildForm()

  With Me
    .Height = 1000
    .Width = 500
    .Padding = 5
    .Arrangement = Arrange.Vertical
    .Center
  End With

  With GridViewData = New GridView(Me) As "GridViewData"
    .Header = GridView.Both
    .Expand = True
    .Columns.Count = 2
    .Columns[0].Title = "Key"
    .Columns[1].Title = "value"
  End With

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

Re: JSON files

Post by DayanUt31 »

Thank you a lot for your help! i was able to understand more about Json decode in gambas.
I made some small adjustements to the code you provide before so instead i can get the data collection printed in a terminal.
This is how it looks:
Public Sub GetData()
  
  Dim myhttp As HttpClient
  Dim sResult, sVal As String
  Dim vResult As Variant
  Dim cCol As Collection
  Dim iRow As Integer
  
  myhttp = New HttpClient
  myhttp.Async = False
  myhttp.Timeout = 60
  myhttp.URL = "https://poloniex.com/public?command=returnTicker"
  myhttp.Get
  
  If Lof(myhttp) Then sResult = Read #myhttp, Lof(myhttp)
  
  vResult = JSON.Decode(sResult)
  
  For Each cCol In vResult
    For Each sVal In cCol
      Print "" & cCol.Key & ": " & sVal
      
    Next
    Print "____________________________"
  Next
  
End
Just one last question.
For some reason, when i try to change the url from this example to this one:
https://jsonplaceholder.typicode.com/users

it gives me an error telling me "Wanted string, got Collection instead"
Image

why is this? and do you know how could i adapt this code to work with this specific Url?
thanks for all the support.
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: JSON files

Post by BruceSteers »

Did you include the "<a href=" parts by mistake?
url strings were added to the URL by the forum and should not be there (Charlie forgot to select the "Do not automatically parse URLs" option)

this is not right...
  myhttp.URL = "<a href="https://poloniex.com/public?command=returnTicker" class="postlink">https://poloniex.com/public?command=returnTicker</a>"
it should just be this..
myhttp.URL = "https://poloniex.com/public?command=returnTicker"
or this even
myhttp.URL = "https://jsonplaceholder.typicode.com/users"
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: JSON files

Post by cogier »

Did you include the "<a href=" parts by mistake?
url strings were added to the URL by the forum and should not be there (Charlie forgot to select the "Do not automatically parse URLs" option)
Opps! :?
I have fixed this.

The reason that the new URL creates a crash is due to the fact that there are Collections inside the Collections and I notice, there is another Collection inside that!
Image

The code below will skip over the error. Programming the code further depends on what your program is expecting to achieve.
' Gambas class file

''*******************************
''REQUIRES gb.net.curl and gb.web
''*******************************

GridViewData As GridView

Public Sub Form_Open()

  BuildForm
  GetData

End

Public Sub GetData()

  Dim myhttp As HttpClient
  Dim sResult As String
  Dim vVal As Variant
  Dim vResult As Variant
  Dim cCol As Collection
  Dim iRow As Integer

  myhttp = New HttpClient
  myhttp.Async = False
  myhttp.Timeout = 60
  myhttp.URL = "https://jsonplaceholder.typicode.com/users"
  myhttp.Get

  If Lof(myhttp) Then sResult = Read #myhttp, Lof(myhttp)

  vResult = JSON.Decode(sResult)

  For Each cCol In vResult
    For Each vVal In cCol
      Inc GridViewData.Rows.Count
      GridViewData[iRow, 0].Text = cCol.Key
      Try GridViewData[iRow, 1].Text = vVal
      Inc GridViewData.Rows.Count
      Inc iRow
    Next
  Next

  GridViewData.Columns.Width = -1

End

Public Sub BuildForm()

  With Me
    .Height = 1000
    .Width = 500
    .Padding = 5
    .Arrangement = Arrange.Vertical
    .Center
  End With

  With GridViewData = New GridView(Me) As "GridViewData"
    .Header = GridView.Both
    .Expand = True
    .Columns.Count = 2
    .Columns[0].Title = "Key"
    .Columns[1].Title = "value"
  End With

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

Re: JSON files

Post by DayanUt31 »

Thank you very much for all the help, i managed to get it running like this:
' Gambas class file

''*******************************
''REQUIRES gb.net.curl and gb.web
''*******************************

GridViewData As GridView

Public Sub Form_Open()
  GetData
  
End

Public Sub GetData()
  
  Dim myhttp As HttpClient
  Dim sResult As String
  Dim vVal As Variant
  Dim vResult As Variant
  Dim cCol As Collection
  Dim iRow As Integer
  
  myhttp = New HttpClient
  myhttp.Async = False
  myhttp.Timeout = 60
  myhttp.URL = "https://jsonplaceholder.typicode.com/users"
  myhttp.Get
  
  If Lof(myhttp) Then sResult = Read #myhttp, Lof(myhttp)
  
  vResult = JSON.Decode(sResult)
  
  For Each cCol In vResult
    For Each vVal In cCol
      Print "" & cCol.Key & "", ": ", vVal
    Next
    Print "-------------------------"
  Next
  
End
The only bad thing is i get different results in the company name. It shows like this "company : (Collection 0x56185d800b98)"
I was thinking i could get around this by adding an if inside the for each so it can change it to Collection or variant depending on the result in vVal but i would need to give it a try.
Post Reply