[fpc-pascal] Copy TJSonArray to Array

Chris Moody inquiry at greensnakedesign.com
Wed Aug 12 18:45:32 CEST 2015



On 08/12/2015 12:36 AM, Michael Van Canneyt wrote:
>
>
> 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.
> _______________________________________________

Hi Michael,

Thanks or the feedback. Just to make sure I'm understanding this, here 
is some more information, part of the JSON feed I'm working with will 
look like this:

"add":["URL1","URL2","URL3"]

My intention was to be able to get the keys under "add" and turn it into 
an array.

So if I'm understanding this correctly, I have to do something like the 
following:

var
J:TJSONData;
A1:TJSONArray;
A2:Array of String;
E : TJSONEnum;

begin
// Put JSONData into J
for E in J do
   if E.Key = 'add' then
       //Somehow copy add from J to A1
      // Use your code to copy A1 into A2
end;


Is this the right way of looking at it?

I have checked what is available online and not finding a lot as far as 
how to work with JSON, hopefully this will improve. Once I finish this 
software, I'd be more than happy to help in donating time for making new 
Wiki pages as far as this subject is concerned.

Thanks again,

Chris





More information about the fpc-pascal mailing list