Help reading a json
Posted: Tue Oct 26, 2021 12:12 am
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())