Page 1 of 1

Insert And Extract JSON Array and List Help

Posted: Sat Apr 11, 2020 8:55 am
by collectordave
Trying to insert an Array and a list into a single JSON file then extract them but hitting trouble.

Can anyone help?

Code: Select all

Structure Entry
  Description.s
  Width.i
EndStructure

Global Dim Album.Entry(10)


Structure Set
  Margin.i
  Border.i
EndStructure

Global  NewList Settings.Set()


Global CurrentFileName.s

CurrentFileName = "Test.wca"



For iLoop = 0 To 9
  
  Album(iLoop)\Description = "Item Number " + Str(iLoop)
  Album(iLoop)\Width = iLoop
  
Next iLoop

AddElement(Settings())
Settings()\Border = 23
Settings()\Margin = 4

If CreateJSON(0)
        iLoop = JSONValue(0)
        InsertJSONArray(iLoop ,Album())
        
       InsertJSONList(iLoop,Settings())
        
        
       SaveJSON(0, CurrentFileName)         
       
       FreeJSON(0)
       
     EndIf
     
     
  If CurrentFileName
    
    iLoop = LoadJSON(#PB_Any, CurrentFileName)
    ExtractJSONArray(JSONValue(iLoop), Album())  
    
   
    
  EndIf
  
  For iLoop = 0 To 9
    
    Debug Album(iLoop)\Description
    
  Next iLoop

Re: Insert And Extract JSON Array and List Help

Posted: Sat Apr 11, 2020 11:45 am
by infratec
I would prefer to make one struct, which make life easy:

Code: Select all

EnableExplicit

Structure Entry
  Description.s
  Width.i
EndStructure


Structure Set
  Margin.i
  Border.i
EndStructure

Structure JSONStructure
  List Settings.Set()
  Array Album.Entry(10)
EndStructure  


Define JSONSave.JSONStructure
Define JSONLoad.JSONStructure
Define CurrentFileName.s, JSON.i, iLoop.i

CurrentFileName = "Test.wca"

For iLoop = 0 To 9
  JSONSave\Album(iLoop)\Description = "Item Number " + Str(iLoop)
  JSONSave\Album(iLoop)\Width = iLoop
Next iLoop

AddElement(JSONSave\Settings())
JSONSave\Settings()\Border = 23
JSONSave\Settings()\Margin = 4

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @JSONSAve, JSONStructure)
  SaveJSON(0, CurrentFileName)
  FreeJSON(0)
EndIf

If CurrentFileName
  JSON = LoadJSON(#PB_Any, CurrentFileName)
  ExtractJSONStructure(JSONValue(JSON), @JSONLoad, JSONStructure)
  FreeJSON(JSON)
EndIf

For iLoop = 0 To 9
  Debug JSONLoad\Album(iLoop)\Description
Next iLoop

Re: Insert And Extract JSON Array and List Help

Posted: Sat Apr 11, 2020 12:15 pm
by collectordave
Thanks Infratec

just done that and it all works well.

CD