Insert And Extract JSON Array and List Help

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Insert And Extract JSON Array and List Help

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Insert And Extract JSON Array and List Help

Post 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
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Insert And Extract JSON Array and List Help

Post by collectordave »

Thanks Infratec

just done that and it all works well.

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply