Help reading a json

Just starting out? Need help? Post your questions and find answers here.
Josepho
Posts: 5
Joined: Mon Sep 13, 2021 9:08 pm

Help reading a json

Post by Josepho »

Hi friends im a bit stuck with something, im trying to read a json that is also generated by my program but im not able, the idea was to save two lists in two different members and then read them back, im having success with the saving but when i want to read it back its not working anyone can help me? here is the code

Code: Select all

Enumeration
  #file_read
  #json_result
  #button_import
  #button_export_xml
EndEnumeration


Structure mazeTile
  toRemove.l
  start.l
EndStructure

Structure pathsMap
  id.l
  cash.l
  List tileids.l()
EndStructure

Global NewList tiles.mazeTile()
Global NewList paths.pathsMap()

Procedure fileReadCall(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
    Debug "Reading.."
    ReadStringFormat(#file_read)
    Input$ = ReadString(#file_read,#PB_UTF8 | #PB_File_IgnoreEOL)
    Debug Input$
    ;  If ParseJSON(#json_result,Input$)
    
    If ParseJSON(#json_result,Input$)
            
      ClearList(tiles())    
      ObjectValue = JSONValue(#json_result)
      ExtractJSONList(GetJSONMember(JSONValue(#json_result),"Tiles"),tiles())
      
      ClearList(paths())
      ExtractJSONList(GetJSONMember(JSONValue(#json_result),"Path"),paths())
      
      Debug ListSize(tiles())
      Debug ListSize(paths())
    Else
      Debug "json read error"
      Debug JSONErrorMessage()
      Debug JSONErrorLine()
      Debug JSONErrorPosition()
    EndIf
    
    Debug IsJSON(#json_result)
  EndIf
  
EndProcedure

Procedure fileImporReaded()
  
  If NextSelectedFile()
    ReadFile(#file_read, SelectedFileID(), @fileReadCall(), #PB_LocalFile | #PB_UTF8)
  EndIf
  
EndProcedure

Procedure GadgetEvent()
  Select EventGadget()

    Case #button_import
      filename$ = OpenFileRequester(".json",@fileImporReaded(),#PB_Requester_MultiSelection)
      
    Case #button_export_xml
      For id = 0 To 10
        AddElement(tiles())
        tiles()\start = Random(10)
        tiles()\toRemove = Random(2)
      Next id
      
      For id = 0 To 10
        AddElement(paths())
        paths()\cash = Random(5)
        paths()\id = Random(3)
        AddElement(paths()\tileids())
        paths()\tileids() = Random(3)
        AddElement(paths()\tileids())
        paths()\tileids() = Random(3)
        AddElement(paths()\tileids())
        paths()\tileids() = Random(3)
      Next id
      
      fileson = CreateJSON(#PB_Any)
      objectValue = SetJSONObject(JSONValue(fileson))
      tilesjson = AddJSONMember(objectValue,"Tiles")
      InsertJSONList(tilesjson,tiles())
      pthsjson = AddJSONMember(objectValue,"Path")
      InsertJSONList(pthsjson,paths())
      ExportJSON(fileson,"demo.json")
      
  EndSelect
  
EndProcedure


OpenWindow(0, 0, 0, 300, 150, "Read file example", #PB_Window_ScreenCentered)
ButtonGadget(#button_export_xml, 10, 10, 280, 30, "export json")
ButtonGadget(#button_import, 10, 40, 280, 30, "read json")
BindEvent(#PB_Event_Gadget, @GadgetEvent())
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Help reading a json

Post by Paul »

Just a personal preference but I find it easier and a lot less work to Insert & Extract as Structures.
Example...

Code: Select all

Enumeration
  #file_read
  #button_import
  #button_export_xml
EndEnumeration


Structure mazeTile
  toRemove.l
  start.l
EndStructure

Structure pathsMap
  id.l
  cash.l
  List tileids.l()
EndStructure

Structure appdata
  List Path.pathsMap()
  List Tiles.mazeTile()
EndStructure

Global dat.appdata




Procedure fileReadCall(Status, Filename$, File, Size)
  Select Status
    Case #PB_Status_Loaded
      Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
      Debug "Reading.."
      ReadStringFormat(#file_read)
      Input$ = ReadString(#file_read,#PB_UTF8 | #PB_File_IgnoreEOL)
      Debug Input$
      
      hJSON= ParseJSON(#PB_Any, Input$)
      If hJSON
        ExtractJSONStructure(JSONValue(hJSON), @dat.appdata, appdata)  
  
        ForEach dat\Tiles()
          Debug dat\Tiles()\start
          Debug dat\Tiles()\toRemove
          Debug "--"
        Next 
        Debug "============="
        ForEach dat\Path()
          Debug dat\Path()\cash
          Debug dat\Path()\id
          ForEach dat\Path()\tileids()
            Debug dat\Path()\tileids()
          Next
          Debug "--"
        Next
                
        FreeJSON(hJSON)
      EndIf
    
    Case #PB_Status_Progress
    
    Case #PB_Status_Error
      Debug "json read error"
      Debug JSONErrorMessage()
      Debug JSONErrorLine()
      Debug JSONErrorPosition()  
        
  EndSelect  
EndProcedure

Procedure fileImporReaded()  
  If NextSelectedFile()
    ReadFile(#file_read, SelectedFileID(), @fileReadCall(), #PB_LocalFile | #PB_UTF8)
  EndIf  
EndProcedure

Procedure GadgetEvent()
  Select EventGadget()

    Case #button_import
      filename$ = OpenFileRequester(".json",@fileImporReaded(),#PB_Requester_MultiSelection)
      
    Case #button_export_xml
      For id = 0 To 10
        AddElement(dat\Tiles())
        dat\Tiles()\start = Random(10)
        dat\Tiles()\toRemove = Random(2)
      Next id
      
      For id = 0 To 10
        AddElement(dat\Path())
        dat\Path()\cash = Random(5)
        dat\Path()\id = Random(3)
        AddElement(dat\Path()\tileids())
        dat\Path()\tileids() = Random(3)
        AddElement(dat\Path()\tileids())
        dat\Path()\tileids() = Random(3)
        AddElement(dat\Path()\tileids())
        dat\Path()\tileids() = Random(3)
      Next id
      
      hJSON=CreateJSON(#PB_Any)
      If hJSON
        InsertJSONStructure(JSONValue(hJSON), @dat, appdata)
        ExportJSON(hJSON,"demo.json")
        FreeJSON(hJSON)
      EndIf
      
  EndSelect
  
EndProcedure


OpenWindow(0, 0, 0, 300, 150, "Read file example", #PB_Window_ScreenCentered)
ButtonGadget(#button_export_xml, 10, 10, 280, 30, "export json")
ButtonGadget(#button_import, 10, 40, 280, 30, "read json")
BindEvent(#PB_Event_Gadget, @GadgetEvent())
Josepho
Posts: 5
Joined: Mon Sep 13, 2021 9:08 pm

Re: Help reading a json

Post by Josepho »

Many thanks i guess i will go with that solution, but it would be also fine if someone can explain me why my code is not working, maybe i should report it as a bug?
Post Reply