Page 1 of 1

LoadJSON with synchrounous problems?

Posted: Fri Dec 04, 2015 10:42 am
by Stefan Schnell
Hello community,

I tried the following code:

Code: Select all

; Begin-----------------------------------------------------------------

    Structure sStatistik
      Jahr.w
      Gemeinde.s
      Einwohnerzahl.i
      maennlich.i
      weiblich.i
      ev.i
      rk.i
      sonstige.i
      Haupt_und_Nebenwohnsitz.i
    EndStructure

    Global Dim Statistik.sStatistik(0)

  ; Sub Main------------------------------------------------------------
    Procedure Main()

      Protected Gemeindestatistik.i, i.i, Ortsgemeinde.i

      If LoadJSON(0, "http://127.0.0.1:9097/Gemeindestatistik.json")
        Gemeindestatistik = GetJSONMember(JSONValue(0), "GEMEINDESTATISTIK.CSV")
        For i = 0 To JSONArraySize(Gemeindestatistik) - 1
          Ortsgemeinde = GetJSONElement(Gemeindestatistik, i)
          ReDim Statistik(JSONArraySize(Gemeindestatistik) - 1)
          If ExamineJSONMembers(Ortsgemeinde)
            While NextJSONMember(Ortsgemeinde)
              Select JSONMemberKey(Ortsgemeinde)
                Case "Jahr"
                  Statistik(i)\Jahr = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "Gemeinde"
                  Statistik(i)\Gemeinde = GetJSONString(JSONMemberValue(Ortsgemeinde))
                Case "Einwohnerzahl"
                  Statistik(i)\Einwohnerzahl = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "maennlich"
                  Statistik(i)\maennlich = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "weiblich"
                  Statistik(i)\weiblich = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "ev."
                  Statistik(i)\ev = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "rk."
                  Statistik(i)\rk = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "sonstige"
                  Statistik(i)\sonstige = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
                Case "Haupt- und Nebenwohnsitz"
                  Statistik(i)\Haupt_und_Nebenwohnsitz = GetJSONInteger(JSONMemberValue(Ortsgemeinde))
              EndSelect
            Wend
          EndIf
        Next
        FreeJSON(0)
      Else
        Debug JSONErrorMessage()
      EndIf

    EndProcedure

  ; Main----------------------------------------------------------------
    Main()

; End-------------------------------------------------------------------
And, as you can see, the loading of the Gemeindestatistik.json is not successfully completed until I get the error from JSONArraySize command.

I think the same behaviour is described here. Or is something wrong with my code?

Thanks for tips and hints.

Cheers
Stefan

Re: LoadJSON with synchrounous problems?

Posted: Fri Dec 04, 2015 12:10 pm
by Fred
You can't load it synchronously. All 'Load' command in SB needs a callback:

Code: Select all

Procedure Loaded(Type, Filename$, ObjectId)
  Static NbLoadedElements
  
  Debug Filename$ + " loaded"
  ; Your code here
  
EndProcedure


Procedure LoadingError(Type, Filename$)
  Debug Filename$ + ": loading error"
EndProcedure


; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loaded())
BindEvent(#PB_Event_LoadingError, @LoadingError())

  
LoadJSON(0, "test.json")

Re: LoadJSON with synchrounous problems?

Posted: Tue Dec 22, 2015 8:57 am
by Stefan Schnell
Hello Fred,

thank you very much for your answer. I correct the code, but I don't have access to the data in the JSON file. I expected at Debug JSONMemberKey(Ortsgemeinde) Jahr, Gemeinde, etc. but I get 0 and 1. What am I doing wrong?

Thanks in advance.
Stefan

Code: Select all

; Begin-----------------------------------------------------------------

  ; Structures----------------------------------------------------------  
    Structure sStatistik
      Jahr.w
      Gemeinde.s
      Einwohnerzahl.i
      maennlich.i
      weiblich.i
      ev.i
      rk.i
      sonstige.i
      Haupt_und_Nebenwohnsitz.i
    EndStructure

  ; Variables-----------------------------------------------------------
    Global Dim Statistik.sStatistik(0)
    Global JSONFile.i = 0

  ; Sub Loaded----------------------------------------------------------
    Procedure Loaded(Type.i, Filename.s, ObjectId.i)

      Protected Gemeindestatistik.i, i.i, Ortsgemeinde.i
  
      Debug Filename + " loaded"
      
      If JSONFile

        Gemeindestatistik = GetJSONMember(JSONValue(JSONFile), "GEMEINDESTATISTIK.CSV")
        ReDim Statistik(JSONArraySize(Gemeindestatistik) - 1)
        For i = 0 To JSONArraySize(Gemeindestatistik) - 1
          Ortsgemeinde = GetJSONElement(Gemeindestatistik, i)
          If ExamineJSONMembers(Ortsgemeinde)
            Debug Str(i) + " " + Ortsgemeinde          
            While NextJSONMember(Ortsgemeinde)
              ;Here I expected Jahr, Gemeinde, etc.
              Debug JSONMemberKey(Ortsgemeinde)
            Wend
          EndIf
        Next

      EndIf

    EndProcedure

  ; Sub LoadingError----------------------------------------------------
    Procedure LoadingError(Type.i, Filename.s)
      Debug Filename + ": loading error"
    EndProcedure

  ; Sub RequesterSuccess------------------------------------------------
    Procedure RequesterSuccess()
      JSONFile = LoadJSON(#PB_Any, NextSelectedFileName(), #PB_LocalFile)
      Debug JSONFile
    EndProcedure

  ; Sub ButtonEvent-----------------------------------------------------
    Procedure ButtonEvent()
      OpenFileRequester("", @RequesterSuccess())
    EndProcedure

  ; Sub Main------------------------------------------------------------
    Procedure Main()

      BindEvent(#PB_Event_Loading, @Loaded())
      BindEvent(#PB_Event_LoadingError, @LoadingError())
    
      If OpenWindow(0, 100, 100, 640, 480, "File")
        ButtonGadget(0, 10, 10, 170, 25, "Open local JSON file...")
        BindGadgetEvent(0, @ButtonEvent())
      EndIf

    EndProcedure

  ; Main----------------------------------------------------------------
    Main()

; End-------------------------------------------------------------------
Here the JSON file:

Code: Select all

{  
   "GEMEINDESTATISTIK.CSV":[  
      {  
         "Jahr":2014,
         "Gemeinde":"Stadt Altenkirchen",
         "Einwohnerzahl":6153,
         "maennlich":2916,
         "weiblich":3237,
         "ev.":2599,
         "rk.":1221,
         "sonstige":2333,
         "Haupt- und Nebenwohnsitz":6431
      },
      {  
         "Jahr":2014,
         "Gemeinde":"Almersbach",
         "Einwohnerzahl":417,
         "maennlich":196,
         "weiblich":221,
         "ev.":222,
         "rk.":62,
         "sonstige":133,
         "Haupt- und Nebenwohnsitz":431
      }
   ]
}

Re: LoadJSON with synchrounous problems?

Posted: Tue Dec 22, 2015 11:58 am
by Peter
Hello Stefan,

i changed you json a little bit. This works for me:

Code: Select all

EnableExplicit

Structure Json_1
  Einwohnerzahl.i
  ev.i
  Gemeinde.s
  Jahr.i
  HauptUndNebenwohnsitz.i
  weiblich.i
  maennlich.i
  sonstige.i
  rk.i
EndStructure

Structure Json
  Array GEMEINDESTATISTIKCSV.Json_1(0)
EndStructure

Global J.Json

Procedure Loaded(Type.i, Filename.s, ObjectId.i)
  
  Debug Filename + " loaded"
  
  ExtractJSONStructure(JSONValue(ObjectId), @J, Json)
  
  Protected Counter
  
  For Counter = 0 To ArraySize(J\GEMEINDESTATISTIKCSV())
    
    Debug "Jahr: " + J\GEMEINDESTATISTIKCSV(Counter)\Jahr
    Debug "Gemeinde: " + J\GEMEINDESTATISTIKCSV(Counter)\Gemeinde
    Debug "Einwohnerzahl: " + J\GEMEINDESTATISTIKCSV(Counter)\Einwohnerzahl
    Debug "HauptUndNebenwohnsitz: " + J\GEMEINDESTATISTIKCSV(Counter)\HauptUndNebenwohnsitz
    Debug "weiblich: " + J\GEMEINDESTATISTIKCSV(Counter)\weiblich
    Debug "maennlich: " + J\GEMEINDESTATISTIKCSV(Counter)\maennlich
    Debug "ev: " + J\GEMEINDESTATISTIKCSV(Counter)\ev
    Debug "rk: " + J\GEMEINDESTATISTIKCSV(Counter)\rk
    Debug "sonstige: " + J\GEMEINDESTATISTIKCSV(Counter)\sonstige
    Debug "----"
    
  Next
  
EndProcedure

; Sub LoadingError----------------------------------------------------
Procedure LoadingError(Type.i, Filename.s)
  Debug Filename + ": loading error"
EndProcedure

; Sub RequesterSuccess------------------------------------------------
Procedure RequesterSuccess()
  LoadJSON(#PB_Any, NextSelectedFileName(), #PB_LocalFile)
EndProcedure

; Sub ButtonEvent-----------------------------------------------------
Procedure ButtonEvent()
  OpenFileRequester("", @RequesterSuccess())
EndProcedure

; Sub Main------------------------------------------------------------
Procedure Main()
  
  BindEvent(#PB_Event_Loading, @Loaded())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
  If OpenWindow(0, 100, 100, 640, 480, "File")
    ButtonGadget(0, 10, 10, 170, 25, "Open local JSON file...")
    BindGadgetEvent(0, @ButtonEvent())
  EndIf
  
EndProcedure

; Main----------------------------------------------------------------
Main()

; End-------------------------------------------------------------------

Code: Select all

{  
   "GEMEINDESTATISTIKCSV":[  
      {  
         "Jahr":2014,
         "Gemeinde":"Stadt Altenkirchen",
         "Einwohnerzahl":6153,
         "maennlich":2916,
         "weiblich":3237,
         "ev":2599,
         "rk":1221,
         "sonstige":2333,
         "HauptUndNebenwohnsitz":6431
      },
      {  
         "Jahr":2014,
         "Gemeinde":"Almersbach",
         "Einwohnerzahl":417,
         "maennlich":196,
         "weiblich":221,
         "ev":222,
         "rk":62,
         "sonstige":133,
         "HauptUndNebenwohnsitz":431
      }
   ]
}
Greetings ... Peter

Re: LoadJSON with synchrounous problems?

Posted: Tue Dec 22, 2015 6:17 pm
by Stefan Schnell
Hello Peter,

thank you very much.
:D

I am impressed about your solution - ten thumbs up.

Cheers
Stefan