It's not a good idea to constantly use ReDim in each iteration. When there are a lot of elements in the LinkedList ReDim is called on each element which means that in the background a lot of copying is be done in the worst case. Don't do that.
Just ReDim once before the loop and use a variable to iterate through the indexes:
Code: Select all
EnableExplicit
Structure JSON
Array Test$(0)
EndStructure
Global NewList Names$()
AddElement(Names$()) : Names$() = "Quin"
AddElement(Names$()) : Names$() = "Sam"
AddElement(Names$()) : Names$() = "Malia"
Define j.JSON, i.i = 0
ReDim j\Test$(ListSize(Names$()) - 1)
ForEach Names$()
j\Test$(i) = Names$()
i + 1
Next
Define JSONID.i = CreateJSON(#PB_Any)
InsertJSONStructure(JSONValue(JSONID), @j, JSON)
Debug ComposeJSON(JSONID)
FreeJSON(JSONID)
For some reason this also works if there are no elements in the LinkedList although ReDim should throw an error because it is called with a negative size.
Edit: FYI: I created another topic regarding the size of -1 here:
https://www.purebasic.fr/english/viewtopic.php?t=86812
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.