[fpc-pascal] Copy TJSonArray to Array

Michael Van Canneyt michael at freepascal.org
Thu Aug 13 09:05:39 CEST 2015



On Wed, 12 Aug 2015, Chris Moody wrote:

>> _______________________________________________
>
> 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?

Yes, but you can do it more simple:

Var
   J : TJSONData;
   MyArray : Array of string;
   MyJSONArray : TJSONArray;
   i : Integer;

begin
   // Put JSONData into J

   MyJSONArray:=(J as TJSONObject).Arrays['add'];
   SetLength(MyArray,MyJSONArray.Count);
   For I:=0 to MyJSONArray.Count-1 do
     MyArray[i]:=MyJSONArray.Strings[i];

end.

>
> 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.

There is a lot of code out there that works with JSON.

I wrote an article long time ago that describes the JSON support:

http://www.freepascal.org/~michael/articles/webdata/webdata.pdf

Page 11 and following.

the fpjson unit is documented:
   http://www.freepascal.org/docs-html/fcl/fpjson/index.html

Hope this helps,

Michael.



More information about the fpc-pascal mailing list