Reading JSON String from structure

Just starting out? Need help? Post your questions and find answers here.
User avatar
Paul
Posts: 197
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Reading JSON String from structure

Post by Paul »

menschmarkus wrote: After first call of Event the debugged values are still undefined. As soon the second call is done the values are correct
This is strange to me. This is a sample of Data from a File. The same happene if I do a HTTPRequest !
Put your Debug lines in the other Procedure.
SpiderBasic hasn't finished running the "ReadLoginFile()" stuff when you request the Debug info.
If you are use to PureBasic where every line is executed one after the other (Procedural), you will have to get use to SpiderBasic and how it runs various parts of your code at the same time (Asynchronous).

Code: Select all

Procedure LeftClickHandler()
  Select GetActiveGadget()
    Case #Button_OpenFile                                                     
      ReadLoginFile()

  EndSelect
EndProcedure

Procedure ReadFileParameter(status.i,FileName.s,File.i,size.i)
  Protected ReadLine.s
    While Not Eof(File)
      ReadLine = ReadString(File) 
      Select UCase(Left(ReadLine,2))
        Case "OW"
          parameter("0")\owner = StringField(ReadLine,2,"=")
        Case "ID"
          parameter("0")\ID = StringField(ReadLine,2,"=")
        Case "PW"
          parameter("0")\Passw = StringField(ReadLine,2,"=")
        Case "AL"
          parameter("0")\al = Val(StringField(ReadLine,2,"="))
        Case "FI"
          parameter("0")\fi = Val(StringField(ReadLine,2,"="))
        Case "PH"
          parameter("0")\phon = Val(StringField(ReadLine,2,"="))
        Case "WZ"
          parameter("0")\wz = Val(StringField(ReadLine,2,"="))
        Case "KY"
          parameter("0")\key = StringField(ReadLine,2,"=")
        Case "NCON"
          parameter("0")\ncon = Val(StringField(ReadLine,2,"="))
          parameter("0")\qual = parameter("0")\ncon
      EndSelect
    Wend
    CloseFile(file)  
    
    Debug parameter("0")\owner
    Debug parameter("0")\ID                                                       
EndProcedure
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Reading JSON String from structure

Post by Peter »

I agree with Paul. Furthermore, I have noticed the following:

Code: Select all

      Select UCase(Left(ReadLine,2))
          [...]
        Case "NCON"
          parameter("0")\ncon = Val(StringField(ReadLine,2,"="))
          parameter("0")\qual = parameter("0")\ncon
      EndSelect
Greetings ... Peter
menschmarkus
Posts: 37
Joined: Thu Apr 10, 2014 3:35 pm

Re: Reading JSON String from structure

Post by menschmarkus »

Put your Debug lines in the other Procedure.
SpiderBasic hasn't finished running the "ReadLoginFile()" stuff when you request the Debug info.
If you are use to PureBasic where every line is executed one after the other (Procedural), you will have to get use to SpiderBasic and how it runs various parts of your code at the same time (Asynchronous).
@Paul
OK, unfortunately the asynchronous running wasn't mentioned before or I did not find it. That explains the delay.
I changed the original source code and check values in the same procedure where I call it. Now it works.

For Newbies in SB, like me, it would be helpful to mention somwhere that procedure calls will run Asynchronious. This information is essential if you come from PB or other languages.

I think problem is solved for me.

Thank you Peter and Paul.
as soon you do it right, it works !
hoerbie
Posts: 100
Joined: Sun Mar 17, 2019 5:51 pm
Location: DE/BY/MUC

Re: Reading JSON String from structure

Post by hoerbie »

Hi,

just to make it clear: NOT every procedure call runs asynchronous in SB.
If you have one procedure running and simply call other procedures in it, all runs the same line after line like in PB.

But: If you use any function for example to load something (HTTPRequest, LoadFile....) or simpler said, where you have to give a "callback function", this callback function is called asynchronous when it has done its work. Simply said, if your program needs to wait on any data, there is a great chance, that you will need a callback function, and this function will be called asynchronous if the data is usable, just like BindEvent in SB and the possible event binding in PB.

Greetings, Hoerbie
Post Reply