[fpc-pascal] Copy TJSonArray to Array

Michael Van Canneyt michael at freepascal.org
Wed Aug 12 09:36:44 CEST 2015



On Wed, 12 Aug 2015, Chris Moody wrote:

> Hi all,
>
> First off thanks to all who have provided help over the past few weeks, I 
> just have one last thing I'm trying to unravel (that I know of currently) and 
> this project should be done.
>
> The JSON feed I'm reading will have 2 arrays in it, would it be best to copy 
> these to a normal array and then process the data? Or best to step through 
> the TJSONArray?

if there is going to be a lot of processing, you might be better off copying the data.
The JSON structure does incur some processing overhead.

>
> I'm not sure how to copy a TJSONArray to an Array, I tried: A:= 
> E.Value.AsArray; but that of course did not work.

A regular loop is simplest:

Var
   MyArray : TMyArray;
   MyJSONArray : TJSONArray;
   i : Integer;

begin
   MyJSONArray:=// Get the array...
   SetLength(MyArray,MyJSONArray.Count);
   For I:=0 to MyJSONArray.Count-1 do
     MyArray[i]:=MyJSONArray.Integers[i];
   // Now work with myarray
end.

Use the correct property for whatever type the array contains.

Michael.



More information about the fpc-pascal mailing list